note 86272 added to function.copy

From: Date: Fri, 10 Oct 2008 07:16:04 +0000
Subject: note 86272 added to function.copy
Groups: php.notes 
Request: Send a blank email to php-notes+get-145240@lists.php.net to get a copy of this message
Thanks SkyEye for a wonderful copy program that I can work with. Being a info-lover, I've changed your datacopy() a bit. I've added more information than just the number of files copied. It now shows Number of files, number of failed files, total amount of data copied (in bytes, i think), and the path to the failed files. It could probably be cleaned up a little, but I'm too lazy to do so, as it is very efficient right now. <?php // 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: [$returnstring =] dircopy($sourcedirectory, $destinationdirectory [, $offset] [, $verbose]); // Example: $num = dircopy('A:\dir1', 'B:\dir2', 1); // Origional by SkyEye. Remake by AngelKiha. // Offset count added for the possibilty that it somehow miscounts your files. This is NOT required. // Remake returns an explodable string with comma differentiables, in the order of: // Number copied files, Number of files which failed to copy, Total size (in bytes) of the copied files, // and the files which fail to copy. Example: 5,2,150000,\SOMEPATH\SOMEFILE.EXT|\SOMEPATH\SOMEOTHERFILE.EXT // If you feel adventurous, or have an error reporting system that can log the failed copy files, they can be // exploded using the | differentiable, after exploding the result string. function dircopy($srcdir, $dstdir, $offset, $verbose = false) { if(!isset($offset)) $offset=0; $num = 0; $fail = 0; $sizetotal = 0; $fifail = ''; 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++; $sizetotal = ($sizetotal + filesize($dstfile)); if($verbose) echo "OK\n"; } else { echo "Error: File '$srcfile' could not be copied!\n"; $fail++; $fifail = $fifail.$srcfile."|"; } } } else if(is_dir($srcfile)) { $res = explode(",",$ret); $ret = dircopy($srcfile, $dstfile, $verbose); $mod = explode(",",$ret); $imp = array($res[0] + $mod[0],$mod[1] + $res[1],$mod[2] + $res[2],$mod[3].$res[3]); $ret = implode(",",$imp); } } } closedir($curdir); } $red = explode(",",$ret); $ret = ($num + $red[0]).",".(($fail-$offset) + $red[1]).",".($sizetotal + $red[2]).",".$fifail.$red[3]; return $ret; } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 74.45.39.106 ---- Manual Page -- http://www.php.net/manual/en/function.copy.php Edit -- https://master.php.net/note/edit/86272 Del: integrated -- https://master.php.net/note/delete/86272/integrated Del: useless -- https://master.php.net/note/delete/86272/useless Del: bad code -- https://master.php.net/note/delete/86272/bad+code Del: spam -- https://master.php.net/note/delete/86272/spam Del: non-english -- https://master.php.net/note/delete/86272/non-english Del: in docs -- https://master.php.net/note/delete/86272/in+docs Del: other reasons-- https://master.php.net/note/delete/86272 Reject -- https://master.php.net/note/reject/86272 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#145240) next »