Forum Moderators: coopster

Message Too Old, No Replies

php recursive copy & permission problem

         

Nadeembabar

11:17 am on Jun 4, 2005 (gmt 0)

10+ Year Member



I am trying to copy a directory with all contents, but after copying the directory, I am unable to delete it. The error received from cute ftp is :
550 Can't remove directory: Permission denied
550 Can't remove directory: Directory not empty

Code is :
#####################################

function copy_dir($source, $destination) {
if (is_file($source)) {
$perm = fileperms($source);
copy($source, $destination);
chmod($destination, $perm);
}
if (is_dir($source)) {
mkdir($destination, 0777);
$dir_handle = opendir($source);
while ($files = readdir($dir_handle)) if( $files!= "." && $files!= "..") $file_array[] = $files;
closedir($dir_handle);
}
for($i=0; $i<count($file_array); $i++) {
$file = $file_array[$i];
if ($destination!= "$source/$file") copy_dir("$source/$file", "$destination/$file");
}
}
##############################

Thanks

coopster

4:41 pm on Jun 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, Nadeembabar.

That codes looks like the *copy directory* part. Are you saying you are creating the source directory via a script and then trying to delete the directory with your FTP client? But you are unsuccessful? That's because the user that created the directory (and subdirectories) is different than the user in your FTP client program.

Do you have shell access? You can login and have a look at the owner/group authorites and you will see they are more than likely different than the userid you use to login via FTP.

I'm not sure which HTTP Server you are using, but PHP (by default) runs as whatever Apache is running as.

Nadeembabar

2:28 am on Jun 5, 2005 (gmt 0)

10+ Year Member



thanks coopster

I understand the problem after reading your reply. I made a little change in the code :

############

$oldmask = umask(0);
mkdir($destination, 0777);
umask($oldmask);

############

Now everything is fine. I don't know why script was not assigning permission to directory without using "umask". Anyhow it was great help.

Another thing, is there any way to apply the same permission from one file/dir to otherone file/dir.

Thanks
Nadeem

coopster

8:32 pm on Jun 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Will any of the the PHP File System Functions [php.net] work for you? Perhaps

chmod [php.net]
chown [php.net]
chgrp [php.net]