Duplicate folder with PHP

I searched all morning for this.

Is there a simple PHP function that will duplicate a folder on my server, temporarily changing permissions on the path, if necessary? Basically an alternative to PHP for me using FTP to copy the entire folder down and then back it up?

I tried the function below, which I found on the Internet, but it does nothing, I think, probably due to permissions. I tried it with error_reporting(E_ALL); , and also checked the return value of each copy() , they all returned false.

 copy_directory('/directory1','/directory2') function copy_directory($src,$dst) { $dir = opendir($src); @mkdir($dst); while(false !== ( $file = readdir($dir)) ) { if (( $file != '.' ) && ( $file != '..' )) { if ( is_dir($src . '/' . $file) ) { copy_directory($src . '/' . $file,$dst . '/' . $file); } else { copy($src . '/' . $file,$dst . '/' . $file); } } } closedir($dir); } 
+4
source share
2 answers

After publishing the generosity, I received a response to the server support ticket, which confirmed my belief that permissions were a problem.

A simple server-side change to give permission to copy PHP resolved this problem.

+6
source

How about checking for duplicate in the code somehow on these lines?

 <?php if(!file_exists($dst)) { mkdir($dst); } else { $i = 1; $duplicate_folder = true; while ($duplicate_folder == true) { if(file_exist($dst) { $new_dst = $dst."_".$i; mkdir($new_dst); $i++; } else { $duplicate_folder = false; } } } ?> 
+1
source

Source: https://habr.com/ru/post/1446188/


All Articles