Permission problem

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
scubadiveflorida
Forum Newbie
Posts: 10
Joined: Wed Oct 29, 2003 9:53 am

Permission problem

Post by scubadiveflorida »

I have a form that that t he client goes in and enters some data. This data is written to a text file in a directory on my server. The only way I can get this to work properly is to open the directory wide open chmod 777 but I am worried about security risks with this. Should I be worried? Is there a better way to do this?

This is what I am using:
$fh = fopen($file, 'w+');
fwrite($fh, $buff);
fclose($fh);



Thanks...
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

[php_man]chmod[/php_man]()

Are you sure 0600 doesn't work? That's the ideal security mode, IMO.

Maybe set the chmod before the script writes, then set it back when its done.

Note: Never tried it, only a theory I thought up just now.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

What is the owner/group of the file and what is the owner/group which php scripts connect under? You can check this by looking at the site with an ftp program and doing a dirinfo command or similar.

I'm guessing that php uses a different owner/group to that under which the file was created. To fix that, create the file with php: thereafter php should be able to write to it with a safer CHMOD 755. You may need to CHMOD 777 the parent folder to allow php to write the file but, once it's written, the parent folder can be CHMOD'd back to 755. Php will be able to write to a file in a 755 folder with a different owner/group if the file itself has the php owner/group.
scubadiveflorida
Forum Newbie
Posts: 10
Joined: Wed Oct 29, 2003 9:53 am

reply

Post by scubadiveflorida »

ok In the function at the begining before being written I am doing:

$permissions = `chmod 777 ../directory_name`;

Then after the fwrite I am doing:

$permissions = `chmod 755 ../directory_name`;


This does not seem to work any ideas?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

is that the exact php code??
scubadiveflorida
Forum Newbie
Posts: 10
Joined: Wed Oct 29, 2003 9:53 am

exact

Post by scubadiveflorida »

$permissions = `chmod 777 /home/users/create`;

# This grabs the correct template then puts the two files together
if ($filename == '')
{
$genericfile = "file.generic";
}else{
$genericfile = "$filename";
}
$buff = get_contents($genericfile);
$buff = $tempvar.$buff;
$file = "$file.txt";
$fh = fopen($file, 'w+');
fwrite($fh, $buff);
fclose($fh);

$permissions = `chmod 755 /home/ams/amsorm_create`;
scubadiveflorida
Forum Newbie
Posts: 10
Joined: Wed Oct 29, 2003 9:53 am

Post by scubadiveflorida »

The script works just not the permission changing
scubadiveflorida
Forum Newbie
Posts: 10
Joined: Wed Oct 29, 2003 9:53 am

Post by scubadiveflorida »

Sorry the bottom $permissions is the following


$permissions = `chmod 755 /home/users/create`
Post Reply