Jump to content


Photo

Copying files from 1 directory to another


  • Please log in to reply
1 reply to this topic

#1 fezzik

fezzik
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 14 October 2006 - 11:44 PM

I'm working on copying files from one directory to another. I'm trying to use a function I found at http://us2.php.net/copy to perform the requested operation. The function is as follows:

// A function to copy files from one directory to another one, including subdirectories and
// nonexisting or newer files. Function returns number of files copied.
// This function is PHP implementation of Windows xcopy  A:\dir1\* B:\dir2 /D /E /F /H /R /Y
// Syntaxis: [$number =] dircopy($sourcedirectory, $destinationdirectory [, $verbose]);
// Example: $num = dircopy('A:\dir1', 'B:\dir2', 1);

function dircopy($srcdir, $dstdir, $verbose = false) {
  $num = 0;
  if(!is_dir($dstdir)) mkdir($dstdir);
  if($curdir = opendir($srcdir)) {
   while($file = readdir($curdir)) {
     if($file != '.' && $file != '..') {
       $srcfile = $srcdir . '\\' . $file;
       $dstfile = $dstdir . '\\' . $file;
       if(is_file($srcfile)) {
         if(is_file($dstfile)) $ow = filemtime($srcfile) - filemtime($dstfile); else $ow = 1;
         if($ow > 0) {
           if($verbose) echo "Copying '$srcfile' to '$dstfile'...";
           if(copy($srcfile, $dstfile)) {
             touch($dstfile, filemtime($srcfile)); $num++;
             if($verbose) echo "OK\n";
           }
           else echo "Error: File '$srcfile' could not be copied!\n";
         }                 
       }
       else if(is_dir($srcfile)) {
         $num += dircopy($srcfile, $dstfile, $verbose);
       }
     }
   }
   closedir($curdir);
  }
  return $num;
}


In my code I call the function:

// move files to temp directory
if (DIRECTORY_SEPARATOR=='/') {
	$absolute_path = dirname(__FILE__).'/';
} else {
	$absolute_path = str_replace('\\\\', '/', dirname(__FILE__)).'/';
}
$absolute_path = str_replace("admin/","",$absolute_path);
echo $absolute_path."<br />"; // "/home/tyler/public_html/hymnal/"
$scrdir = $absolute_path."uploads/".$categoryInfo['category_path']."/";
echo $scrdir."<br />"; // "/home/tyler/public_html/hymnal/uploads/652900643/"
$dstdir = $absolute_path."uploads/temp/";
echo $dstdir."<br />"; // "/home/tyler/public_html/hymnal/uploads/temp/"
// call move directory function
$num = dircopy($scrdir, $dstdir, 1);
echo $num."<br />";

I use the above code to find the absolute path and then strip "admin/" from the path because the directory "uploads/" resides on the same level as "admin/". The dircopy() function is not returning any errors so I'm not sure why it is not coping the files. I've included print statements to verify the information but I'm still unsuccessful in finding the problem. Any suggestions are appreciated.

Cheers,

Fezzik

#2 fezzik

fezzik
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 15 October 2006 - 04:38 AM

Nevermind. I have a different recursive script that I'm using now.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users