views:

142

answers:

3

Hi i need to remove % sign from file or image name in directory which string i use

$oldfile = "../wallpapers/temp-uploaded/".$file ;
$newfile = "../wallpapers/temp-uploaded/". trim( str_replace('%', '', $file));

rename("$oldfile","$newfile");

But its not work reply me which string i use ( trim, str_replace not work preg_replace how can i use for remove &%$ etc reply back

+3  A: 

It could be an issue with other things as your logic seems correct. Firstly

rename("$oldfile","$newfile");

should be:

rename($oldfile,$newfile);

and:

$oldfile = "../wallpapers/temp-uploaded/".$file ;

should be:

$oldfile = '../wallpapers/temp-uploaded/'.$file ;

as there is no need for the extra interpolation. Will speed things up. Source: http://j.mp/9whyj3 (See "double (") vs. single (') quotes"). And http://j.mp/b7MEX0

In regards to the problem, you have to do some proper debugging:

  • Does echo "[$oldfile][$newfile]"; look as expected
  • Make sure the folder and oldfile exists.
  • Does var_dump(file_exists($oldfile),file_exists($newfile)) output true, false
  • Does file_get_contents($oldfile); work?
  • Does file_put_contents($newfile, file_get_contents($oldfile));
  • Make sure you have write permissions for the folder. Typically chmod 777 will do.
  • Before the rename, perform: if ( file_exists($newfile) ) { unlink($newfile); } as you will have to delete the newfile if it exists, as you will be moving to it. Alternatively, you could append something to the filename if you do not want to do a replace. You get the idea.

In regards to the replace question.

As you have said you would like %xx values removed, it is probably best to decode them first:

$file = trim(urldecode($file));

You could use a regular expression then:

$newfile = '../wallpapers/temp-uploaded/'.preg_replace('/[\\&\\%\\$\\s]+/', '-', $file); // replace &%$ with a -

or if you want to be more strict:

$newfile = '../wallpapers/temp-uploaded/'.preg_replace('/[^a-zA-Z0-9_\\-\\.]+/', '-', $file); // find everything which is not your standard filename character and replace it with a -

The \\ are there to escape the regex character. Perhaps they are not needed for all the characters I've escaped, but history has proven you're better safe than sorry! ;-)

balupton
i need this if %$# etc in image name then replace it to -or _how do this?
Hassan
@balupton: Why should double quotes be converted to single quotes? Double quotes are just as correct imo.
Znarkus
@user395167 - I've updated the post to include regular expressions, this is the best way. You have the choice of just what you are after, and a more strict variation.@Znarkus. I've updated the post to include the reason why, and a source for reference.
balupton
But its not remove % from file or image file name image file name is 5035%2C.jpg how to remove Percatage sign?
Hassan
and need to remove space " " also in file orimage file
Hassan
Updated the post, to include urldecode for the % removal. If you would like them to be removed, rather than replaced, then change the second paramater of preg_replace to '' instead. The PHP manual can tell you this - it's actually A WONDERFUL THING that manual! ;-)Also updated the first regex to remove spaces as well. The PHP manual also has excellent documentation about regexes so well worth reading. And reading is much easier than asking sometimes ;-)
balupton
its great .. but i want to remove space " " also in file name or Image name
Hassan
I added that request to the above :S. It's the "\\s" in regex means space/tab/newline
balupton
Use @balupton's "more strict" one. It will remove anything that isn't a-zA-Z0-9_-. characters.
Blair McMillan
please in $file = trim(urldecode($file)); what i do in this trim string thats remove also Spaces// //in imagename or file name?
Hassan
I think you can figure that one out yourself @user395167 ;-) You've got more than enough to material to figure it out. Wouldn't be much of an educator if I just give you all the answers now would I? :-)
balupton
i am a beginner please help me on this removing spaces using $file = trim(urldecode($file)); please help me
Hassan
just write code only
Hassan
+2  A: 
$file = trim($file);
$oldfile = "../wallpapers/temp-uploaded/".$file ;
$newfile = "../wallpapers/temp-uploaded/".str_replace('%', '', $file);

rename($oldfile,$newfile);
Sadat
A: 

To replace &%$ in the filename (or any string), I would use preg_replace.

$file = 'file%&&$$$name';
echo preg_replace('/[&%$]+/', '-', $file);

This will output file-name. Note that with this solution, many consecutive blacklisted characters will result in just one -. This is a feature ;-)

Znarkus