1

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++;
         }
      }
     }
AL-zami
  • 8,902
  • 15
  • 71
  • 130
  • So if I got you right you want to take all files (Only images?) from `c:/xampp/htdocs/practice/haha` and move them into `c:/xampp/htdocs/practice/haha/tour/FILE extension/image$i.jpg` ? – Rizier123 Feb 25 '15 at 14:09
  • edited my post.Pathinfo() function is removed form rename() function.FIle extension is omitted in case you didn't notice – AL-zami Feb 25 '15 at 14:11
  • Ah you updated the code :D now I see it so, but you only want to move images? into `c:/xampp/htdocs/practice/haha/tour/image$i.jpg` and all `files/images? from c:/xampp/htdocs/practice/haha` ? – Rizier123 Feb 25 '15 at 14:25

2 Answers2

1

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
1

This should work for you:

Here I just get all images from your directory with glob(). I create the directory if it doesn't exist already with mkdir() and then move all images with rename().

<?php

    $dir = "c:/xampp/htdocs/practice/haha";
    $files = glob($dir . "/*.{jpg,png,gif,jepg}", GLOB_BRACE);


    //Create directory
    if (!file_exists($dir . "/tour")) {
        mkdir($dir . "/tour");         
    } 

    //Move all images
    foreach($files as $key => $file) {
        rename($dir . "/" .$data, $dir . "/tour/image" . ($key+1) . ".jpg");
    }

?>
Rizier123
  • 58,877
  • 16
  • 101
  • 156