note 72380 added to function.copy

From: Date: Wed, 17 Jan 2007 00:01:16 +0000
Subject: note 72380 added to function.copy
Groups: php.notes 
Request: Send a blank email to php-notes+get-121818@lists.php.net to get a copy of this message
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

« previous php.notes (#121818) next »