Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Created file | SSH Can't delete (permission denied)

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);
    }
like image 826
RichardW11 Avatar asked Nov 04 '22 02:11

RichardW11


1 Answers

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
like image 159
mabe.berlin Avatar answered Nov 07 '22 21:11

mabe.berlin