Nll
Nll

Reputation: 880

How to change Filename in URL

I have two field in database "FileName" and "UrlFileName",so I sanitize "FileName" from Invalid character to have a new file Name for example "FinaleFileName",so my question is I need a simple code or function to change the name of file in URL too before stored my both field in DB,for example :

$FileName = "NaMeoFImagE-10.jpeg"
$UrlFileName = "uploads/pics/NaMeoFImagE-10.jpeg"
....
//I had created a function to remove or replace a invalid character //Ok

$FinaleFileName= nameofimage-10.jpeg

.....

A function to replace a File name in URL // Not OK

$FinaleUrlFileName = "uploads/pics/nameofimage-10.jpeg"// Im looking for this result 

Any Idea?

Upvotes: 0

Views: 1361

Answers (3)

Yaseen
Yaseen

Reputation: 1358

I don't know for sure if this is what you want, in addition to it, this is a quick-and-dirty solution.

    <?php

         $FileName = "nameofimage-10.jpeg";  //your sanitized file name
         $UrlFileName = "uploads/pics/NaMeoFImagE-10.jpeg";  //unclean URl

 $parts = explode("/", $UrlFileName);
 $name = $parts[ count($parts) - 1] ;
 $name = $FileName;
 $final = "";  //final cleaned URL

 for($i = 0; $i < count($parts) - 1; $i++)
 {
    $final .= $parts[$i] . "/";
 }
 $final .= $name;

  echo $final  //return the clean URL;
?>

Is this what you want ?

Upvotes: 0

Gordon Bailey
Gordon Bailey

Reputation: 3911

I'm not sure I understand what you're asking, but based on your test case, I think you're looking for strtolower.

Upvotes: 2

Fluffeh
Fluffeh

Reputation: 33502

If you are going to use URLfilename to search for finalFilename then yes, they will need to be the same, else you won't find your files. If URLFileName is only going to be used to search for URLFileName, then it won't make any difference (as long as you check it for injection attacks on your DB).

Having said that, why keep both? Why not just cleanse the URLFilename and use that and only that? Why double up the information?

Upvotes: 0

Related Questions