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! ;-)