[英]PHP cannot rename files
I want to rename a couple of files that I have read and sorted beforehand.我想重命名我事先阅读和排序的几个文件。 But unfortunately, the php script runs but the files are not renamed.
但不幸的是,php 脚本运行但文件没有重命名。 Any idea why.
知道为什么。 Paths are correct.
路径是正确的。 I have also made sure that I have permissions for each file with chmod.
我还使用 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);
}
?>
This is the error I am getting:这是我得到的错误:
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
It looks like you forgot to put the folder name in your other if statements, you only refer to $directory in the first if statement:看起来您忘记将文件夹名称放在其他 if 语句中,您只在第一个 if 语句中引用 $directory :
if($i==0){
$newName = $regNo."_BOUND_TOP_R.tif";
// Does this one work? It has $directory.
rename($directory.$file, $newName);
}else if($i==1){
// but the rest do not
$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++;
}
and as per the comment from jibsteroos, fix your concatenators to .
并根据 jibsteroos 的评论,将您的连接器修复为
.
, not +
: , 不是
+
:
$newName = $regNo."_f0000"+$no+"V.tif";
should be应该
$newName = $regNo . '_f0000' . $no . 'V.tif';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.