【发布时间】:2020-04-05 22:26:39
【问题描述】:
我想重命名几个我事先阅读和排序的文件。但不幸的是,php 脚本运行但文件未重命名。知道为什么。路径是正确的。我还确保使用 chmod 对每个文件都有权限。
<?php
$directory = ($_POST['path']);
$filecount = 0;
$files = glob($directory . "*");
if ($files){
$filecount = count($files);
}
$allDetails = explode("/", $directory);
$regNo=($allDetails[1]);
$files = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if(($file != ".") and ($file != "..")) {
$files[] = $file; // put in array.
}
}
natsort($files); // sort.
$i = 0;
$no = 1;
foreach($files as $file) {
chmod($file, 0777);
if($i==0){
$newName = $regNo."_BOUND_TOP_R.tif";
rename($directory.$file, $newName);
}else if($i==1){
$newName = $regNo."_BOUND_TOP_V.tif";
rename($file, $newName);
}else if($i==$filecount-2){
$newName = $regNo."_BOUND_BOTTOM_R.tif";
rename($file, $newName);
}else if($i==$filecount-1){
$newName = $regNo."_BOUND_BOTTOM_V.tif";
rename($file, $newName);
}else if($i%2 == 0){
$newName = $regNo."_f0000"+$no+"R.tif";
rename($file, $newName);
$no++;
} else if($i%2 == 1){
$newName = $regNo."_f0000"+$no+"V.tif";
rename($file, $newName);
$no++;
}
$i++;
}
closedir($handle);
}
?>
这是我得到的错误:
Warning: rename(00002-scan_2019-11-19_09-34-37l.tif,R40_BOUND_TOP_V.tif): No such file or directory in /var/www/html/CanvasCreator/renameImages.php on line 40
【问题讨论】:
-
错误是什么意思?开启 error_reporting 了吗?
-
抱歉,我是 PHP 新手...在哪里可以看到 error_reporting?
-
看看how-do-i-get-php-errors-to-display....顺便说一句,您发布的代码是灾难性的...至少考虑一下数组映射而不是所有那些“ifs”和“elses”
-
您可能还想检查
chmod($file, 0777);实际返回的内容。如果是false,则 chmod 失败(如果您没有对它的写入权限,它会执行此操作,并且还会导致重命名失败。) -
对于字符串连接使用
.,而不是+,例如$newName = $regNo."_f0000"+$no+"V.tif";