I found this piece of code by Rich Rodecker (http://www.visible-form.com/blog/copy-directory-in-php/). It works fine apart from if there are files in the folder, it converts them into folders.
Here is the PHP snippet from the link above.
function copyr($source, $dest){// Simple copy for a file
if (is_file($source)) {
$c = copy($source, $dest);
chmod($dest, 0777);
return $c;
}
// Make destination directory
if (!is_dir($dest)) {
$oldumask = umask(0);
mkdir($dest, 0777);
umask($oldumask);
}
// Loop through the folder
$dir = dir($source);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == "." || $entry == "..") {
continue;
}
// Deep copy directories
if ($dest !== "$source/$entry") {
copyr("$source/$entry", "$dest/$entry");
}
}
// Clean up
$dir->close();
return true;
}
copyr("copy","copy2");
For example This is the current structure of the site
- Root- - index.php (Code here that runs copy function)
- - copy (DIR)
- - - index.html (Dummy content in file)
When I run the index.php it then creates this:
- Root- - index.php (Code here that runs copy function)
- - copy (DIR)
- - - index.html (HTML)
- - copy2 (DIR)
- - - index.html (DIR)
- - - - EMPTY
Can anyone see what the problem is and provide a solution? I want to be able to specify a directory and have it back up the entire directory, including sub folders and files.
From PHP manual:
Note that is_file() returns false if the parent directory doesn't have +x set for you; this make sense, but other functions such as readdir() don't seem to have this limitation. The end result is that you can loop through a directory's files but is_file() will always fail.
Check permissions for the source directory.