Jump to content

Moving Multiple Files


mikebrady81

Recommended Posts

Hey!

 

I keep getting this message:

 

Warning: copy() [function.copy]: The first argument to copy() function cannot be a directory in /Library/WebServer/Documents/RandysWebsite/newgallery.php on line 80

 

Here's the code:

 

foreach($filenames as $value){

copy('/Library/WebServer/Documents/RandysWebsite/gallery/' . $foldername . '/' . $value . '',  '/Library/WebServer/Documents/RandysWebsite/' . $foldername . '/images/' . $value . '');

}

 

The line that starts with "copy" is line 80.

 

What i'm trying to do:

 

I have a folder "gallery" with random images that I won't always know the names of prior. I'm trying to move all of the images from gallery to "variable folder name/images/

 

Make sense?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/195013-moving-multiple-files/
Share on other sites

are you sure all the contents are files?

 

you may need to put something like

 

foreach($filenames as $value){
   if(!is_dir('/Library/WebServer/Documents/RandysWebsite/gallery/' . $foldername . '/' . $value))
   {
         copy('/Library/WebServer/Documents/RandysWebsite/gallery/' . $foldername . '/' . $value,  '/Library/WebServer/Documents/RandysWebsite/' . $foldername . '/images/' . $value);
   }

}

 

also I cleaned up the code a bit, you had some quotes in there you didn't really need.

 

http://us.php.net/manual/en/function.is-dir.php

 

as the error says you can't copy a directory.. so you need to copy the files inside

 

try something like this

$dir = "/from/folder/";
$dirto = "/to/folder/";
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if($file == "."||$file == "..") continue;
            copy($dir.$file, $dirto.$file);
        }
        closedir($dh);
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.