note 72380 deleted from function.copy by colder
| From: | colder@php.net | Date: | Wed, 17 Jan 2007 00:18:35 +0000 |
| Subject: | note 72380 deleted from function.copy by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-121820@lists.php.net to get a copy of this message | ||
Note Submitter: code at ashleyhunt dot co dot uk
Reason: bad code
----
Okay,
I had problems copying whole (long) directory structures in one go so I have included the mkpath()
function to create an entire directory structure that does not yet exist.
Also upadated the dircopy() function to work on windows as well as unix with he inclusion of
DIRECTORY_SEPARATOR
Tested and works well on Apache/2.2.2 (Win32) DAV/2 with PHP/5.1.4
<?
dircopy("data/39/2006/11/04/e233743", "cddump/data/39/2006/11/04/e233743",
true);
function mkpath($path) {
$dirs=array();
$path=preg_replace('/(\/){2,}|(\\\){1,}/','/',$path); //only forward-slash
$dirs=explode("/",$path);
$path="";
foreach ($dirs as $element)
{
$path.=$element."/";
if(!is_dir($path))
{
if(!mkdir($path)){ echo "something was wrong at : ".$path; return 0; }
}
}
return true;
}
function dircopy($srcdir, $dstdir, $verbose = false) {
$num = 0;
mkpath($dstdir);
if($curdir = opendir($srcdir)) {
while($file = readdir($curdir)) {
if($file != '.' && $file != '..') {
$srcfile = $srcdir . DIRECTORY_SEPARATOR . $file;
$dstfile = $dstdir . DIRECTORY_SEPARATOR . $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;
}
?>