简体   繁体   中英

PHP Rename and illegal filename

I have a strange problem with renaming an existing file:

I have on my server a series of html files that need to be selected via a file selection dialog and then renamed.

The form that opens the dialog is called via JavaScript and correctly opens the dialog, allows a file to be selected, and renames it, right up until I come across a file with some (presumably) illegal characters in it. At this point I get a file not found error.

The source and destination for the rename are the same, it is only the filename that needs to be changed.

My code, which works on other files is as follows (with variables renamed for some clarity):

$dest_path = realpath(dirname(dirname(__FILE__))) . $miDestination ;
$targefile = $dest_path  .  basename( $_FILES["thesrcfile"]["name"]);
$newName = $dest_path . $anewname . ".html";
rename($targefile,$newName);

As mentioned this works fine until I reach a file with a name such as:

These are "very" strangely-named file's!.html

From this I get a file not found error. All permissions are correct and this code has been working fine with more sensibly named files. By the way, I have no control over the names of the files and they can quite literally contain any characters.

Can anyone suggest a way to get the rename function to recognise these files as existing, and to rename them accordingly?

many thanks

Max

trim()str_replace()所有非法字符char放入$ anewname var

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM