I've got a website at


Which has a couple of files, one of which is:

-rwxr-xr-x  1 root root      1609 Feb 16  2010 send_form_email.php

(earlier it was 644)

I can access the file fine from the web, but with the error in apache:

PHP Warning: file_put_contents(contacts.txt) [function.file-put-contents]: failed to open stream: Permission denied in /var/www/html/send_form_email.php on line 47, referer: http://www.mysite.com/

What should I be changing? The file ownership? Contacts.txt?


If contacts.txt exists, then the permissions on contacts.txt needs to be changed, probably to 666. If it doesn't, then the permissions on the directory you want it to be in needs to be changed to allow the webserver to write to it.

The first way is the way you want to do it, especially if there's anything else in the directory you don't want to let web users write over.

  • The first didnt work -rw-rw-rw- 1 root root 3381 Oct 6 10:13 contacts.txt No change. What should I change the fokder to? – siliconpi Oct 15 '10 at 19:36
  • Are you sure your script is using the correct path to the file? Try adding an var_dump file_exists("contacts.txt"); and see if it says true. If not, use the complete path to the file (eg /var/www/files/contacts.txt) In order to create a new file in a directory, the directory needs to be writable by the webserver user, but doing so would make any file in the directory deletable/replaceable by any script running. Also, out of curiosity, do you have PHP's safe mode enabled? I don't know what kind of errors it would cause (I don't use it) but it does mess with using files. – DerfK Oct 15 '10 at 21:11
  • It returns: bool(true) and safe-mode is off – siliconpi Oct 16 '10 at 11:28
  • If you're using just "contacts.txt" maybe there's another file in PHP's search path that it's trying to use, try using the full path to the file? Check and make sure the directory is something like rwxr-xr-x if it's owner/group is root root. It might work with something like rwx-r-x--x too. x on a directory gives permission to be "in" the directory, r gives permission to get the file listing. If contacts.txt is in the same dir as your php script then it must already have access, or it couldn't read the php code in the first place. If its not, every directory up to the file needs x – DerfK Oct 16 '10 at 13:05
  • There's no other contacts.txt . And the permissions for 'html' folder is as: drwxr-xr-x 9 root root 4096 Oct 16 05:52 html – siliconpi Oct 16 '10 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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