Search code examples
phprenamemkdir

rename images and move the renamed images in newly created directory


Here i want to create a new directory called c:/xampp/htdocs/haha/tour/ and in the directory i want to move my renamed images .Here ,i managed to create the new directory but can't move and rename my images.How can i solve this problem??

$dir='c:/xampp/htdocs/practice/haha';

$i=1;
if(is_dir($dir)){
echo dirname($dir).'</br>';
     $file=opendir($dir);

     while(($data=readdir($file))!==false){
      if($data!='.' && $data!='..'){

            $info=pathinfo($data,PATHINFO_EXTENSION);

            if(!file_exists($dir.'/tour')){

                    mkdir($dir.'/tour/');

            }

            rename($dir.$data,$dir.'/tour/'.'image '.$i.'.jpg');

            $i++;
         }
      }
     }

Solution

  • You're missing some /:

    rename($dir.$data,$dir.'/tour/'.'image '.$i.'.jpg');
               ^---
    

    $data doesn't contain ANY /, so what you're building is

    rename('c:/xampp/htdocs/practice/haha' . 'foo', etc...)
    

    which becomes

    rename('c:/xampp/htdocs/practice/hahafoo', etc...)
                                     ^^^^^^^---doesn't exist
    

    Try

       rename($dir .'/' . $data,$dir.'/tour/'.'image '.$i.'.jpg');
                  ^^^^^^^^
    

    instead.