Weird permission problem after a copy()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SilverTab
Forum Newbie
Posts: 3
Joined: Fri Jan 30, 2004 7:52 pm

Weird permission problem after a copy()

Post by SilverTab »

Ok i'm using those 2 simple functions to copy an entire directory:
---------------------------------------------
function cp($wf, $wto){ // it moves $wf to $wto
if (!file_exists($wto)){
mkdir($wto,0777);
}
$arr=ls_a($wf);
foreach ($arr as $fn){
if($fn){
$fl="$wf/$fn";
$flto="$wto/$fn";
if(is_dir($fl)) cp($fl,$flto);
else copy($fl,$flto);
echo "$fl to $flto<br>";
}
}
}

///////////////////////////////////////////////////
/// ls_a function////////////////////////
// This function lists a directory.
// ANd is needed for the cp function.

function ls_a($wh){
if ($handle = opendir($wh)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." ) {
if(!$files) $files="$file";
else $files="$file\n$files";

}
}
closedir($handle);
}
$arr=explode("\n",$files);

return $arr;
}
-----------------------------------------
Everything's working, and the directory is copied, the problem is that I don't have any permissions on the copied files after the copy is done....I can't rename/delete or even CHMOD them..(not only in php, but via FTP also) so now i'm stuck with the copied files on my server and there's nothing I can do... I tried on 2 different servers and got the same result...!
User avatar
Pointybeard
Forum Commoner
Posts: 71
Joined: Wed Sep 03, 2003 7:23 pm
Location: Brisbane, AUS
Contact:

Post by Pointybeard »

Yea i have been having the same problems with an image gallery i wrote. I found that after copying the file, in an FTP program i had no permissions on that file at all. I have managed to fix this by chmod() the file in php after it is copied, setting them to 644 or somthing. That allowd me to do stuff to them in FTP prog. Also you seem to be able to use unlink() to delete files and you dont get any probs.

Well thats what i have found anyway. I would like to know y you cant do anything with the files in the FTP tho if you dont do any of the above...

-PB
SilverTab
Forum Newbie
Posts: 3
Joined: Fri Jan 30, 2004 7:52 pm

Post by SilverTab »

Pointybeard wrote:Yea i have been having the same problems with an image gallery i wrote. I found that after copying the file, in an FTP program i had no permissions on that file at all. I have managed to fix this by chmod() the file in php after it is copied, setting them to 644 or somthing. That allowd me to do stuff to them in FTP prog. Also you seem to be able to use unlink() to delete files and you dont get any probs.

Well thats what i have found anyway. I would like to know y you cant do anything with the files in the FTP tho if you dont do any of the above...

-PB
Thanks a bunch! everything worked exactly as you said...it's fine if I chmod it right after....and Unlink and rmdir was working to remove the files...might be because ftp and php are two different "users" seems to be a conflict or something....weird! But thanks again!
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

SilverTab wrote:might be because ftp and php are two different "users" seems to be a conflict or something....weird! But thanks again!
That's exactly it. Ftp'ing files and folders gives them a certain owner/group. Files folders created with php have another owner/group (eg nobody/nobody on my ISP webspace, or php/php or whatever).

Setting all permissions 0777 lets anything do anything anywhere but is only a temporary solution.

You can, for example, open up a parent directory with CHMOD 777 (in your ftp program), write (say) a file with php, and then CHMOD the directory back to something more secure.

Php is the owner of the file it just wrote so, regardless of the parent folder permissions, it can continue to write to it.

But, of course, your ftp program can't do anything with it (depending on permissions).

We should do a phpdn tutorial on all this sometime - it's a common headache.

If you do a dirinfo (or similar) with your ftp program you can see what all the owner/groups are for your files & folders.

These fns might be useful (you'll need to take out the $this-> refs since I just grabbed them from a class).

Code: Select all

<?php

    function info($path) 
	{
        if (is_writable($path)) 
		{
            $this->report .= $path . ' is writable.<br />';

        } else {

            $this->report .= $path . ' is not writable.<br />';
        }
        
        $this->report .= 'fileperms (octal): ' . $this->_filePermsOctal($path) . '<br />';
        $this->report .= 'fileowner: ' . fileowner($path) . '<br />';
        $this->report .= 'filegroup: ' . filegroup($path) . '<br />';
        $this->report .= 'owner name: ' . @posix_getpwuid(fileowner($path)) . '<br />';
        $this->report .= '<hr />';
    }
    

    function _filePermsOctal($path) 
	{
        $decperms = fileperms($path);
        $octalperms = sprintf("%o",$decperms);
        return (substr($octalperms,2));
    }


?>
Last edited by McGruff on Tue Aug 09, 2005 8:45 pm, edited 1 time in total.
SilverTab
Forum Newbie
Posts: 3
Joined: Fri Jan 30, 2004 7:52 pm

Post by SilverTab »

McGruff wrote:
SilverTab wrote:might be because ftp and php are two different "users" seems to be a conflict or something....weird! But thanks again!
That's exactly it. Ftp'ing files and folders gives them a certain owner/group. Files folders created with php have another owner/group (eg nobody/nobody on my ISP webspace, or php/php or whatever).

Setting all permissions 0777 lets anything do anything anywhere but is only a temporary solution.

You can, for example, open up a parent directory with CHMOD 777 (in your ftp program), write (say) a file with php, and then CHMOD the directory back to something more secure.

Php is the owner of the file it just wrote so, regardless of the parent folder permissions, it can continue to write to it.

But, of course, your ftp program can't do anything with it (depending on permissions).

We should do a phpdn tutorial on all this sometime - it's a common headache.

If you do a dirinfo (or similar) with your ftp program you can see what all the owner/groups are for your files & folders.

These fns might be useful (you'll need to take out the $this-> refs since I just grabbed them from a class).
Thanks! It confirms what I tought, and help me to understand it a bit better :)
Post Reply