Search code examples
phpfile-rename

rename all the images in a specified directory


I have around 600 images which I want to rename with a small PHP code

These are the image files

I've tried this link below and modified it to my own desires:

rename all the image files in a specified directory

<?php
$dir = 'C:/xampp/htdocs/rename/fotos/all_fotos';
    $file = opendir($dir);
    $data = readdir($file);

    if(is_dir($dir)){
        echo $dir . '</br>';
        echo $file . '</br>';
        echo $data . '</br>';
        while(($data = readdir($file)) !== false){
            $info = pathinfo($data, PATHINFO_EXTENSION);
            if($info=='jpg'){
                if(strlen($data)==5){
                    rename($data, $dir . $data = 3 . substr($data, 0, -4) . '.jpg');
                }else if(strlen($data)==6){
                    rename($data, $dir . $data = substr($data, 0, -4) . '.jpg');
                }else{
                    echo "renaming didn't work, please try again <br/>";
                }
            }else{
                echo 'selected file is not a .jpg extension <br/>';
                echo basename(pathinfo($data, PATHINFO_BASENAME),'.jpg') . '</br>';
            }
        }echo $info;
    }else{
        echo dirname($dir) . 'wrong directory </br>';
    }
    closedir($file);
    ?>

expected results would be for example: '12340' to become '31234' and if the length is 6 for example: '123450' it's supposed to become '12345', however the second "echo $file" inside the first if() part returns a "Resource ID #3"

The current output


Solution

  • It could be interesting to be more clear about the outputs using echo and var_dump(). First point : the rename() function needs fullpath for its both arguments (see official doc : https://www.php.net/manual/fr/function.rename.php). Second : to get the string-name of the file, you need the basename() function (https://www.php.net/manual/fr/function.basename.php). Third point : you havent taken the extension ".jpg" into account in strlen...=5 or strlen...=6. Last point : using relativ path for the directory (relativ to your php renaming-script):

    //$dir = 'C:/xampp/htdocs/rename/fotos/all_fotos';
    $dir = __DIR__.'/renameimg';
    $file = opendir($dir);
    $data = readdir($file);
    
    if(is_dir($dir)){
    echo '</br>dir : '.$dir;
    echo '</br>file : '.$file;
    echo '</br>data : '.$data;
    
     while(($data = readdir($file)) !== false){
     echo "<br/>";
     var_dump($data);
      if(is_dir("$dir/$data"))
          echo "<br/>Directory: ".$data;
       else if(is_file("$dir/$data"))
       echo "<br/>File: ".$data;
    
        $info = pathinfo($data, PATHINFO_EXTENSION);
        if($info=='jpg'){
      $filename = basename($data);
      echo "<br/>data length : ". strlen($data);
      echo "<br/>filename=basename(data) length : ". strlen($filename);
            if(strlen($filename)==9){
             rename("$dir/$data", "$dir/3".substr($filename, 0, -4) . '.jpg');
            }else if(strlen($filename)==10){
                rename("$dir/$data", "$dir/".substr($filename, 0, -4) . '.jpg');
            }else{
            echo "<br/>Length is not 5 or 6 : renaming didn't work !";
            }
        }else{
            echo 'selected file is not a .jpg extension <br/>';
        echo basename(pathinfo($data, PATHINFO_BASENAME),'.jpg').'</br>';
        }
       }echo $info;
    }else{
    echo dirname($dir) . 'wrong directory </br>';
    }
     closedir($file);