1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
function copy_directory($source,$destination ) {
if ( is_dir( $source ) ) {
@mkdir( $destination );
$directory = dir( $source );
while ( FALSE !== ( $readdirectory = $directory->read() ) ) {
if ( $readdirectory == '.' || $readdirectory == '..' ) {
continue;
}
$PathDir = $source . '/' . $readdirectory;
if ( is_dir( $PathDir ) ) {
copy_directory( $PathDir, $destination . '/' . $readdirectory );
continue;
}
copy( $PathDir, $destination . '/' . $readdirectory );
}
$directory->close();
}else {
copy( $source, $destination );
}
}
function copy_dir ($from, $to, $reccursive = true, array & $files = null) {
if (!is_dir($from) || !is_readable($from))
throw new InvalidArgumentException("First parameter is not a valid directory or is not readable");
if (!is_dir($to) || !is_writable($to))
throw new InvalidArgumentException("Second parameter is not a vadlid directory or is not writeable");
$from = realpath($from);
$to = realpath($to);
if ($reccursive) {
$dir = new RecursiveDirectoryIterator($from);
$dir = new RecursiveIteratorIterator($dir);
}
else {
$dir = new DirectoryIterator($from);
}
$files = array();
foreach ($dir as $item) {
$dirname = realpath($item->getPath());
$filename = realpath($item->getPathname());
$new_dirname = str_replace($from, $to, $dirname);
$new_filename = str_replace($from, $to, $filename);
if (!$reccursive && $item->isDir())
continue;
elseif(!isset($files[$new_dirname]) && !is_dir($new_dirname))
$files[$new_dirname] = mkdir($new_dirname);
$files[$new_filename] = copy($filename, $new_filename);
}
return !in_array(false, $files, true);
}
set_time_limit(0);
define('SOURCE', 'D:/temp/test/1');
define('DESTINATION', 'D:/temp/test/%s');
echo "<pre>";
for ($i = 0; $i < 10; $i++) {
echo "TEST #$i\n";
mkdir($dest_a = sprintf(DESTINATION, "$i.1"));
mkdir($dest_b = sprintf(DESTINATION, "$i.2"));
_st();
copy_directory(SOURCE, $dest_a);
echo "copy_directory() took " . _nd() . " seconds\n";
_st();
copy_dir(SOURCE, $dest_b);
echo "copy_dir() took " . _nd() . " seconds\n";
} |
Partager