[英]Why PHP rename() is deleting files?
我无法确定我的代码出了什么问题。 这行代码将删除文件,而不是重命名文件。
for($i=0;$i<(7-$n);$i++){
rename($location."/".($last-$i).".txt", $location."/".($last-$i-$n).".txt");
rename($location."/".($last-$i).".bmp", $location."/".($last-$i-$n).".bmp");
}
我已经回显了源文件名和目标文件名,对我来说它看起来是正确的。 我正在使用unlink()函数执行相同的“变量沙拉”,并且其行为符合预期。 我试图制作自己的rename()函数:
function myRename($old, $new){
$content = printFile($old); //this function returns the content of a file.
unlink($old);
$file = fopen($new, "w");
fwrite($file,$content);
fclose($file);
}
发生相同的错误。 所有重命名的文件将被删除。 那让我觉得我做的“可变沙拉”有一个愚蠢的错误。 但是我不知道是什么。 回显的结果对我来说是正确的。 PHP没有给我任何有关该问题的警告。
回显结果:
来源:“ data / directory_exemple / 47.txt”目标:“ data / directory_exemple / 46.txt”
谢谢您的帮助!
编辑:看起来只有文件之一被重命名。 其余的将被删除。 也许是逻辑错误。
您下面的功能正常工作
function myRename($old, $new){
$content = printFile($old); //this function returns the content of a file.
unlink($old);
$file = fopen($new, "w");
fwrite($file,$content);
fclose($file);
}
我已经用示例文件测试了您的myRename()
函数,它会重命名。
确保您具有正确的路径,从printFile()
获取内容并具有写权限
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.