0

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
Katia
  • 81
  • 1
  • 10
  • 1
    What are the errors saying? Turned on error_reporting? – B001ᛦ Dec 12 '19 at 13:51
  • Sorry I am new to PHP...where I can I see the error_reporting? – Katia Dec 12 '19 at 13:53
  • 1
    Look at [how-do-i-get-php-errors-to-display](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display/21429652#21429652).... By the way the code you have posted is catastrophic...think at least about array mapping instead of all those "ifs" and "elses" – B001ᛦ Dec 12 '19 at 13:53
  • 1
    You might also want to check what `chmod($file, 0777);` actually returns. If it's `false`, then the chmod failed (which it would do if you don't have write access to it and would also cause the renaming to fail.) – M. Eriksson Dec 12 '19 at 13:54
  • 1
    for string concatenation use `.`, not `+` in e.g. `$newName = $regNo."_f0000"+$no+"V.tif";` – jibsteroos Dec 12 '19 at 13:57
  • I think the chmod is not working as I need to use sudo to execute the function from terminal. Can I use sudo as well from php? – Katia Dec 12 '19 at 13:57
  • @Katia - Make sure that the user that runs PHP from your server (usually www-data on nix systems) have proper access to them from the start instead. – M. Eriksson Dec 12 '19 at 14:00
  • I believe you need to add the directory to the path like you do in the first clause: `rename($directory.$file)` – devb Dec 12 '19 at 14:13

1 Answers1

0

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($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 ., not +:

$newName = $regNo."_f0000"+$no+"V.tif";

should be

$newName = $regNo . '_f0000' . $no . 'V.tif';
delboy1978uk
  • 12,118
  • 2
  • 21
  • 39