Search code examples
phpimagewarningsfile-rename

Warnings 5 and 32 on first two files while renaming all files in a directory


I made a simple image renaming application. It adds a number serially after every image name to distinguish one from another. But the problem is that, when this PHP application runs I get two warnings and I think for this reason $i value is starting with 3, like image 3, image 4….

enter image description here But I want it to start it with 1. Like image 1, image 2….

And the warning I got is

enter image description here

So my question is:

  1. Why are these warnings generated?

  2. How to execute this PHP code without getting any warnings?

    $dir='c:/xampp/htdocs/practice/haha/'; echo getcwd().''; $i=1; if(is_dir($dir)){ echo dirname($dir); $file=opendir($dir);

     while(($data=readdir($file))!==false){
      $info=pathinfo($data,PATHINFO_EXTENSION);
        echo $data;
         //echo pathinfo($data,PATHINFO_BASENAME).'</br>';
         //echo basename(pathinfo($data,PATHINFO_BASENAME),'.jpg').'</br>';
         rename($dir.$data,$dir.'mat '.$i.'.jpg');
          echo $i.'</br>';
         $i++;
    
     }
    

    }


Solution

  • Exclude the implicit . and .. automatic directory entries from the loop, since they can't be renamed anyway. That's where the errors are coming from; the . (current directory) is of course in use in your own code, and .. (parent directory) can't be renamed when a child is open.