DankMemes
DankMemes

Reputation: 2137

PHP deleting a file from a subdirectory?

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?

Upvotes: 1

Views: 782

Answers (3)

rodeone2
rodeone2

Reputation: 109

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.

Upvotes: 1

Marian Zburlea
Marian Zburlea

Reputation: 9427

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)

Upvotes: 4

ILikeTacos
ILikeTacos

Reputation: 18676

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.

Upvotes: 1

Related Questions