views:

126

answers:

4

I want to move all files from one folder to other. my code is following in this i made a folder in which i want to copy all file from templats folder

          $doit = str_replace (" ", "", $slt['user_compeny_name']);
                   mkdir("$doit");
             $source="templat/";
          $target=$doit."/";
              $dir = opendir($source);
             while (($file = readdir($dir)) !== false)
                {
              copy($source.$file, $target.$file);
                     }

It working fine . copy all files but give warning that The first argument to copy() function cannot be a directory

can any one help me asap

+3  A: 

if ($file != "." && $file != "..") { // copy }

Alexey
+4  A: 

Readdir will read all children in a directory, including other dirs, and 'virtual' dirs like . and .. (link to root and parent dir, resp.) You'll have to check for these and prevent the copy() function for these instances.

while (($file = readdir($dir)) !== false)
{
    if(!is_dir($file))
    {
        copy($source.$file, $target.$file);
    }
}
Duroth
+3  A: 

You are not accounting for the . and the .. files at the top of the directory. This means that the first thing it tries to copy is "\template." which would be the same as trying to copy the directory.

Just add something like:

if ($file !== "." && $file !== "..")
...
Anthony
+3  A: 

opendir() will include items . and .. as per the documentation.

You will need to exclude these by using the code in the other comments.

jezmck
thanks! now it is working fine
Rajanikant