Linux SSH
I create a file in php using
if (!is_dir(DIR_FILE))
mkdir(DIR_FILE, 0777);
$filename = DIR_FILE . $id . '.txt';
$handle_cf = fopen($filename, 'a');
fwrite($handle_cf, $data . "\n");
fclose($handle_cf);
chmod($filename, 0777);
chown($filename, "usr111"); // usr111 = username
chgrp($filename, "usr111"); // usr111 = group that is also attached to apache
The file gets the following permissions.
-rwxrwxrwx 1 apache apache 1447 Apr 4 12:48 D.txt
-rwxrwxrwx 1 apache apache 1447 Apr 4 12:48 E.txt
however when I try to delete the file, under the regular user account (usr111). I get the following error
[usr111@host session]$ rm D.txt
rm: cannot remove `D.txt': Permission denied
NOTE: I can delete the file under root.
FIX FOUND! even though I was using the mode setting on mkdir for php. For some reason this wasn't working. I added the following.
if (!is_dir($dir)) {
mkdir($dir, 0777);
chmod($dir, 0777);
}
mkdir is working well but the second argument isn't the permission it's a mode that will be used by the system together with your current umask to calculate the permissions to set. From manual:
The mode is also modified by the current umask, which you can change using umask().
You need to change your script to set the permissions without calling the filesystem twice:
$oldUmask = umask(0); // disable umask
mkdir($path, 0777);
umask($oldUmask); // reset the umask
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With