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. Any idea why. Paths are correct. I have also made sure that I have permissions for each file with 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