Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to delete JPEG files that were uploaded to the server via FTP. The files are uploaded and written with permissions of 644. The owner and group of the upload directory are mike and mike.

I have tried changing the owner and group to www-data, but that does not seem to work.

I am trying to delete the files with a PHP script using unlink(). This works on the production server (which is a hosting service), but not my development server, which is a LAMP setup. This leads me to believe it has something to do with permissions on my development server.

Do you think this has something to do with PHP or Apache configuration?

share|improve this question
up vote 3 down vote accepted

To delete a file on Unix-like systems, you need write permission on the directory containing it, not the file itself. This is because the underlying operation is actually modifying the directory by removing (unlinking) an entry from it.

In order to do what you want to do, the directory containing the file you want to delete must be writable by www-data.

share|improve this answer
The only exception to this explanation is that the file has the sticky bit on. – Federico Fenara May 19 '10 at 15:29
True. The sticky bit (+t) will prevent deletion unless you also own the file. – James Sneeringer May 19 '10 at 15:40
@James For the directory to be writable by www-data, should I use chown www-data /var/www/test/images? – Mike Moore May 19 '10 at 17:17
@James Well, I was able to make it work by changing the User and Group within /etc/apache2/apache2.conf to User www-data and Group www-data, and then using chown www-data:www-data /var/www/test/images to the upload folder. BUT, the owner of /var/www/test/images had previously been set to mike. So, changing it to www-data now prevents me from uploading to that folder via FTP. Any suggestions? – Mike Moore May 19 '10 at 18:16
This is the approach I take for things like this. First, chown it back to mike. Next, create a new group (e.g. groupadd uploads). Then, change the directory's group to the new group with chgrp uploads _dir_ and give the group write access with chmod g+w _dir_. Finally, add www-data to the group with gpasswd -a www-data uploads. This may require a restart of Apache. This way, mike retains access since he owns it, and members of the www-data group can also write to it. Use chmod +t _dir_ to prevent www-data from deleting mike's files. – James Sneeringer May 19 '10 at 21:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.