1

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?

1 Answer 1

3

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.

6
  • The only exception to this explanation is that the file has the sticky bit on. May 19, 2010 at 15:29
  • 1
    True. The sticky bit (+t) will prevent deletion unless you also own the file. May 19, 2010 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, 2010 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, 2010 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. May 19, 2010 at 21:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .