Directory copying?
I need to copy some directories with PHP, so I did a few searches and found a few scripts to try, considering that there is no built in method in PHP. I ran one of them and got an error about the maximum running time of the script being exceeding. Thinking this was because some of the files were too big, I increased that time. Little did I know, the script was recursively copying the directory and then copying it inside itself... I'm still in the process of deleting everything it made...
Anyway, I was hopin开发者_开发知识库g someone on here would have a reliable script they've written or know about for this type of thing.
edit: hadn't occurred to me that copying a directory to inside itself wasn't a good idea. Sorry whoever wrote the code I just trashed.
<?php
function copy_directory($source, $destination, $whatsgoingon = false){
if ($destination{strlen($destination) - 1} == '/'){
$destination = substr($destination, 0, -1);
}
if (!is_dir($destination)){
if ($whatsgoingon){
echo "Creating directory {$destination}\n";
}
mkdir($destination, 0755);
}
$folder = opendir($source);
while ($item = readdir($folder)){
if ($item == '.' || $item == '..'){
continue;
}
if (is_dir("{$source}/{$item}")){
copy_dir("{$source}/{$item}", "{$destination}/{$item}", $whatsgoingon);
}else{
if ($whatsgoingon){
echo "Copying {$item} to {$destination}"."\n";
}
copy("{$source}/{$item}", "{$destination}/{$item}");
}
}
}
?>
You can use:
copy_directory('./directory', './directory2/'); // copy the directory
copy_directory('./directory', './'); // copy the files to the same directory
copy_directory('./directory', './directory2/', true);
// Show these messages:
// Creating directory ./directory2
// Copying word.docx to ./directory2
// Copying test.txt to ./directory2
精彩评论