简体   繁体   English

PHP重命名和非法文件名

[英]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. 我的服务器上有一系列的html文件,需要通过文件选择对话框选择它们,然后重命名。

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. 通过JavaScript调用打开对话框的表单,并正确打开对话框,允许选择一个文件并重命名它,直到我遇到其中包含一些(可能是)非法字符的文件为止。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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