How to remove% -sign in php string

I need to remove the% sign from the file name or image in the directory on which line I use

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

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

But it doesnโ€™t work answer me which line I use (trim, str_replace doesnโ€™t work preg_replace, how can I use &% $ to delete etc. answer back

+3
source share
3 answers

This might be a problem with other things, as your logic seems to be correct. Firstly,

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

it should be:

rename($oldfile,$newfile);

and

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

it should be:

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

since there is no need for additional interpolation. Will accelerate. Source: Benchmark test (see "Double (") vs. single (") quotes"). here .

,, :

  • echo "[$oldfile][$newfile]";
  • , .
  • var_dump(file_exists($oldfile),file_exists($newfile)) true, false
  • file_get_contents($oldfile);?
  • file_put_contents($newfile, file_get_contents($oldfile));
  • , . chmod 777.
  • : if ( file_exists($newfile) ) { unlink($newfile); }, , , . , - , . .

.

, % xx, :

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

:

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

:

$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 -

\\, . , , , , , !;-)

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

rename($oldfile,$newfile);
+2

To replace &%$with a file name (or any string), I would use preg_replace.

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

This will exit file-name. Please note that with this solution, many consecutive blacklists will have only one -. This is the function :-)

0
source

Source: https://habr.com/ru/post/1755187/


All Articles