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

Go To StackoverFlow.com

5

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);
    }
2012-04-04 17:59
by RichardW11
what are the permissions on the directory this file is in - Marc B 2012-04-04 18:00
If you're logging in with a regular user account, PHP or rather Apache is the file owner and you won't be able to delete it unless it's got permissions for everyone to do that. Otherwise, if you're logged in with root, you should have no issues - Francisc 2012-04-04 18:01
+MarcB drwxr-xr-x 2 apache apache 4096 Apr 4 12:48 sessio - RichardW11 2012-04-04 18:02
+Francisc Is there a way for me to make this user able to delete apache files in it's home folder - RichardW11 2012-04-04 18:03
Since there's no write permission on the folder for your user or group, you can't remove files from the folder (or rename files). You could solve this using ACLs - wimvds 2012-04-04 18:06
+MarcB & wimvds. I fixed the issue by doing the php chmod() after the mkdir command. even though it was set. Some reason wasn't working. This did - RichardW11 2012-04-04 18:07
777 is not a good idea on shared hosting.. - Guilherme Viebig 2012-04-04 18:23
It been opened by another proces - Baba 2012-04-04 18:26
@RichardW11: mkdir applies the umask to the mode you provide, that's probably why it didn't work - wimvds 2012-04-04 19:30


2

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
2012-04-05 12:37
by mabe.berlin
Thanks for the explanation - RichardW11 2012-04-05 16:26
Ads