This is a PHP function that copy an entire specified directory/folder includes its sub directories/folders and files to another folder/directory.
1. PHP Copy Entire Directory Recursively
<?php 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 ); } } ?> |
2. Usage Copy Entire Directory Function
<?php copy_directory("D:/dev/web/htdocs/4rapiddev/database", "D:/dev/web/htdocs/4rapiddev/backup/database"); copy_directory("D:/dev/web/htdocs/4rapiddev/logs", "D:/dev/web/htdocs/4rapiddev/backup/logs"); ?> |
Note:
- 1. The destination directory/folder must have write permission or in a writable directory
- 2. After copy the directory to another place, we may need to completely delete this in order to save our web space.