1

I have a PHP script that deletes files. It can delete files from my root directory, but when I try to delete from a subdirectory, it says "Permission denied". I know the file exists and PHP can access it because I can read and write to the file, but I can't delete it. Why?

EDIT: If this is relevant, I am using Zymic hosting. But I have another site on Zymic where the deleting works fine. I don't get it...

EDIT: I use ajax to access the PHP file with the code to delete, and the ajax sends the file name to delete. I know the file name it sends is correct, because the warning message prints it for me. The PHP code is simply:

$file=$_POST['file'];
echo unlink($file);

EDIT: I fixed it! I don't know why this worked, but I FTP-chmodded the directory from 755 to 775 Can anyone tell me why it worked?

4
  • 1
    What are the permissions on the file and the directory? Are you using a mandatory access control tool such as AppArmor, SELinux, TOMOYO, or SMACK? Any of these could prevent deleting a file. Check dmesg(1) output and /var/log/audit/audit.log for messages that may be related. Commented May 4, 2012 at 22:16
  • @sarnold why don't you post that as an answer? Commented May 4, 2012 at 22:17
  • No I checked to permissions. As I said, I can read and write to the file. Commented May 4, 2012 at 22:18
  • @jjclarkson: because it is a gigantic guess. I think I'm at about 30% with this particular guess... :) Commented May 4, 2012 at 22:23

3 Answers 3

4

To delete the file you need write permissions to the folder that contains it, check that first.

CHMOD xxx -> Owner Group Other

first case: 755 - Owner (read, write, execute), Group (read, execute), Other (read, execute)

second case: 775 - Owner (read, write, execute), Group (read, write, execute), Other (read, execute)

Sign up to request clarification or add additional context in comments.

4 Comments

He mentioned that can write to file.
@debianek: writing to the file has no relationship to being able to delete the file.
Chmodding the directory seemed to work. But why? I could already write to the file...
@Zove: Because when you link or unlink a file from a directory with rename(2), open(2) with O_CREAT, or unlink(2), you're modifying the directory, not the file.
1

Try to add this at the beginning of the script you're running:

error_reporting(E_ALL | E_STRICT);

That should be able to point out accurately what is going on, chances are that you do not have permissions to write to the folder

Especially if you're working on a Linux environment. In Linux everything is a file, even folders. When it comes to deleting files, you need to be able to write to the file that represents a folder, that's why having permissions to write to the file you're trying to get rid of, does not have anything to do with deleting it.

1 Comment

I know! I said I fixed it! I chmodded the folder to 775
1

You have to fclose($myfile) first before using unlink($myfile),,because if it is open on the server by anyone it will not delete it. Also place this script in the same directory as the files you wish to delete,, otherwise you may accidently delete the whole DIR.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.