0

I am working on some code that will take all the images in one folder and copy them into another folder and then delete the original folder and it content.

I have:

copy('images/old-folder/*', 'images/new-folder/');
unlink('images/old-folder/');

but this does not work :( but it doesn't work I mean the files dont copy over and the old-folder is not deleted :(

I even tried:

system('cp images/old-folder/* images/new-folder/');

and that didn't work either :( Please help.

I have even tried to change the permissions of the two folders:

chmod('images/old-folder/', 0777);
chmod('images/new-folder/', 0777);
user3723240
  • 395
  • 3
  • 11
  • 29
  • Have you tried a fully qualified path? Does the account have permissions? – Andy Jones Jul 25 '14 at 15:20
  • `copy()` is not supposed to be working on directories but single files only, as said in the documentation. Please see [this answer](http://stackoverflow.com/a/7775949/180709) for a pure PHP solution. If you can, please don't use shell calls that have to be used with extreme caution. – zopieux Jul 25 '14 at 15:22

3 Answers3

3
foreach(glob('images/old-folder/*') as $image) {
    copy($image, 'images/new-folder/' . basename($image)); unlink($image);
}
rmdir('images/old-folder');

check the docs: glob, rmdir, also you might find user comments on rmdir useful.

EDIT:

added basepath to the second parameter to the copy function which has to be an actual path and not a directory.

Prasanth
  • 5,230
  • 2
  • 29
  • 61
0
<?php

$src = 'pictures';
$dst = 'dest';
$files = glob("pictures/*.*");
      foreach($files as $file){
      $file_to_go = str_replace($src,$dst,$file);
      copy($file, $file_to_go);
      }

?>

Found here: PHP copy all files in a directory to another?

Community
  • 1
  • 1
Bradly Spicer
  • 2,278
  • 5
  • 24
  • 34
0

Here is a modified version from @Prasanth that should work (not tested)

<?php
    $oldfolder = 'images/new-folder';
    $newfolder = 'images/old-folder';

    $files = glob($oldfolder . '/*');

    foreach($files as $file){
        $filename = basename($file);
        copy($file,  $oldfolder . '/' . $filename);
        unlink($file);
    }
    rmdir($oldfolder);
?>
MilMike
  • 12,571
  • 15
  • 65
  • 82