How can PHP functions be utilized to differentiate between files and directories when duplicating recursively via FTP?
When duplicating files and directories recursively via FTP using PHP, you can utilize the `is_dir()` function to differentiate between files and directories. By checking if a given path is a directory before duplicating its contents, you can ensure that only directories are recursively duplicated.
function duplicateFilesRecursively($source, $destination) {
if(is_dir($source)) {
if(!is_dir($destination)) {
mkdir($destination);
}
$files = scandir($source);
foreach($files as $file) {
if($file != '.' && $file != '..') {
duplicateFilesRecursively($source . '/' . $file, $destination . '/' . $file);
}
}
} else {
copy($source, $destination);
}
}
// Usage
duplicateFilesRecursively('/path/to/source/directory', '/path/to/destination/directory');
Keywords
Related Questions
- What are the potential risks of using outdated PHP functions like mysql_* instead of more secure alternatives like MySQLi or PDO?
- What are the advantages and disadvantages of using cookies instead of sessions for storing user data in a PHP application, particularly in a gaming scenario?
- How can prepared statements be effectively used in PHP to prevent SQL injection and improve database performance?