Mombu the Php Forum sponsored links

Go Back   Mombu the Php Forum > Php > note 72380 added to function.copy
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 25th February 15:08
code
External User
 
Posts: 1
Default note 72380 added to function.copy



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;
}

?>
----
Server IP: 69.147.83.197
Probable Submitter: 89.241.191.245
----
Manual Page -- http://www.php.net/manual/en/function.copy.php
Edit -- https://master.php.net/note/edit/72380
Del: integrated -- https://master.php.net/note/delete/72380/integrated
Del: useless -- https://master.php.net/note/delete/72380/useless
Del: bad code -- https://master.php.net/note/delete/72380/bad+code
Del: spam -- https://master.php.net/note/delete/72380/spam
Del: non-english -- https://master.php.net/note/delete/72380/non-english
Del: in docs -- https://master.php.net/note/delete/72380/in+docs
Del: other reasons-- https://master.php.net/note/delete/72380
Reject -- https://master.php.net/note/reject/72380
Search -- https://master.php.net/manage/user-notes.php
  Reply With Quote


  sponsored links


2 25th February 15:08
colder
External User
 
Posts: 1
Default note 72380 deleted from function.copy by colder



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;
}

?>
  Reply With Quote


  sponsored links


Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666