View Full Version : auto copy directory and all file inside, help?

01-22-2010, 03:35 AM
Hi.. All members

i need your full suggestions and Thanks a lot and sorry for bothering.. I'm still a PHP rookie

i looking for php code how to auto copy directory and all file inside, to another directory and also auto change the directory/file permission after copied.

i found the similar from this forum but that not enough to solve my problem. because there no auto change the directory/file permission after copied

echo "done";

function copydir($source,$destination)
$oldumask = umask(0);
mkdir($destination, 01777); // so you get the sticky bit set
$dir_handle = @opendir($source) or die("Unable to open");
while ($file = readdir($dir_handle))
if($file!="." && $file!=".." && !is_dir("$source/$file"))


Len Whistler
01-22-2010, 05:48 AM
The key term to CHMOD all files in a directory is "recursively". I did a quick Google search and found this:


A further search might turn up something better.


01-22-2010, 08:52 AM
Thank so much you give a helpful link, here i posted the code:

function chmodDirectory( $path = ‘.’, $level = 0 ){
$ignore = array( ‘cgi-bin’, ‘.’, ‘..’ );
$dh = @opendir( $path );
while( false !== ( $file = readdir( $dh ) ) ){ // Loop through the directory
if( !in_array( $file, $ignore ) ){
if( is_dir( “$path/$file” ) ){
chmodDirectory( “$path/$file”, ($level+1));
} else {
chmod(”$path/$file”,0777); // desired permission settings
}//if in array
closedir( $dh );
as i said at the my first posted.. i am new rookie to php, by seeing the two code that may possible to joint them but i don't know how to do.
any help for me would be great thank you.

how i want it work?
first, let say i have a directory "data"
the "data" directory it self already cmod to 755, and i want to copy "data" directory including all file inside, let say become "data1" and then auto cmod the "data1" to 644 after copy finished
that possible?


Len Whistler
01-22-2010, 06:42 PM
I did a quick test with the code below and it appears to be OK, I got it from the PHP manual at http://php.net/manual/en/function.copy.php.

Call this function using the last line of code at bottom.

function smartCopy($source, $dest, $folderPermission=0755,$filePermission=0644){
# source=file & dest=dir => copy file from source-dir to dest-dir
# source=file & dest=file / not there yet => copy file from source-dir to dest and overwrite a file there, if present

# source=dir & dest=dir => copy all content from source to dir
# source=dir & dest not there yet => copy all content from source to a, yet to be created, dest-dir

if (is_file($source)) { # $source is file
if(is_dir($dest)) { # $dest is folder
if ($dest[strlen($dest)-1]!='/') # add '/' if necessary
$__dest .= basename($source);
else { # $dest is (new) filename
$result=copy($source, $__dest);
elseif(is_dir($source)) { # $source is dir
if(!is_dir($dest)) { # dest-dir not there yet, create it
if ($source[strlen($source)-1]!='/') # add '/' if necessary
if ($dest[strlen($dest)-1]!='/') # add '/' if necessary

# find all elements in $source
$result = true; # in case this dir is empty it would otherwise return false
while($file=readdir($dirHandle)) { # note that $file can also be a folder
if($file!="." && $file!="..") { # filter starting elements and pass the rest to this function again
# echo "$source$file ||| $dest$file<br />\n";
$result=smartCopy($source.$file, $dest.$file, $folderPermission, $filePermission);
else {
return $result;

// call function with this line
smartCopy("source_folder_name", "dest_folder_name");



01-23-2010, 03:48 AM
well, many thank for your great tips code, it seem work at my desktop with win-XP HE + XAMP, but i don't test it in web server yet, so i don't know if the cmod work or not...

hm... now would you go on to the next...? adding command button (submit button), mean we need it run (execute) by pushing the submit button.

<form method="post" name="frm" action="copy.php"/> <tr><td><input name="Send" type="submit" value="Submit" /></td></tr> </form>';
copy.php it self as your current code (smartcopy). that's i am wrong? or do you /someone else have any great idea?

Len Whistler
01-23-2010, 06:13 AM
Both files in the same folder.


<form method="post" name="frm" action="copy.php"/> <tr><td><input name="Send" type="submit" value="Submit" /></td></tr> </form>';

Add this line at the end of the code posted in post #4 it will redirect you back to some_page.php once the php has been executed.

header("Location: some_page.php");

NOTE: for the redirect to work you can't have any white space or html outputted. I think that code in post #4 will be OK.