2

I want to delete deleteme.txt in the Ubuntu web server.

So I made 4.php do the following:

<?php
unlink('deleteme.txt');
?>

deleteme.txt has the following permission status:

-rwxrwxrwx 1 ubuntu ubuntu    19 Jun 12 06:18 deleteme.txt

When I execute "4.php", this error always occurs

Warning: unlink(deleteme.txt): Permission denied in /var/www/html/4.php on line 2

I already tried chmod 777 deleteme.txt and chown ubuntu /var/www/html on the directory which contains "deleteme.txt"

I also tried chown ubuntu /var/www/ on the parent directory of that file.

Nick Udell
  • 2,420
  • 5
  • 44
  • 83
user3734451
  • 21
  • 1
  • 2
  • 3
    Do you have write permission to the directory the file is in? – Majenko Jun 12 '14 at 14:39
  • 1
    Maybe try an absolute path? unlink('/var/www/deleteme.php'); – Kver Jun 12 '14 at 14:40
  • 1
    chowning to ubuntu is pointless if this script is not running as the ubuntu user, or doesn't have write pmermissions to start with. – Marc B Jun 12 '14 at 14:50
  • @kver it doesn't work. Warning: unlink(/var/www/html/deleteme.txt): Permission denied in /var/www/html/4.php on line 2 – user3734451 Jun 12 '14 at 15:19
  • @Majenko I didn't realize the write permission in the directory the file is in is the troublemaker for me. I typed "chmod 777 /var/www/html" and the delete operation worked perfectly. Thank you so much! – user3734451 Jun 12 '14 at 15:37
  • When you delete a file you're not writing to the file, you're writing to the directory the file is in - useful thing to remember. – Majenko Jun 12 '14 at 15:39
  • @Majenko Thank you so much. If you were in my house, I will buy a cool beer for you. – user3734451 Jun 12 '14 at 15:50

1 Answers1

1

you need to chown to www-data, thus meaning that the www-data will gain ownership of the file allowing you to delete it through unlink with php.

Like so:

$ chown www-data <file or folder>
achedeuzot
  • 4,164
  • 4
  • 41
  • 56
annoymous
  • 11
  • 1
  • Thank you. I had needed to change mode of the directory into capable of writing. I just changed the owner of the directory. but that doesn't do. writing permission of not only files but also "the present Directory the files is in" – user3734451 Jun 12 '14 at 16:04