PHP重命名文件包含特殊字符“/”和“”

时间:2017-01-06 22:34:13

标签: php file-rename

如何在PHP中重命名名称中包含“/”的文件 例如:“A / B”

$filename= "A / B";
rename("1.html", $filename.".html"); 

不起作用!

2 个答案:

答案 0 :(得分:1)

在Linux机器和OSX上,正斜杠(/)是一个禁止的字符。文件系统不允许您在文件名中使用此字符。

答案 1 :(得分:0)

我尝试了并且可以修改字符串,但正如其他用户所说......可能/您将遇到操作系统的问题。因为操作系统的树使用了/ caracter。

/                 --> is root
/folderX          --> is the folde X in the root folder
/folderX/yourFile --> is the file in the folderX in the root folder

这是我在website

上运行的代码
<?php
$filename = "this/is/a/test";

$filename = str_replace("/", "-", $filename);
var_dump($filename)

?>

这是结果

string(14) "this-is-a-test"

如您所见,这是一个名为test this/is/a/test

的文件的路径

这是一个没有路径this-is-a-test

的文件名

我希望这个答案可以帮助你看到差异。