8

Possible Duplicate:
file_put_contents permission denied

I have recently transferred server, and it seems that file_put_contents is not working on the new server.

Everything is the same, the folders are chmodded correctly, but for some reason it is not creating the files and putting the contents in it.

I have created a test for you to view, emulating how we are currently doing it:

file_put_contents("/home/user/public_html/test/test.progress", "test");

the script is being ran at

/home/user/public_html/test.php

/test folder is chmodded to 755 (777 makes no difference)

I am getting the following error:

Warning: file_put_contents(/home/user/public_html/test/test.progress) [function.file-put-contents]: failed to open stream: Permission denied in /home/user/public_html/test.php on line 2

Do I need to change any settings on the server for this to work? What is wrong?

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
Latox
  • 4,655
  • 15
  • 48
  • 74

1 Answers1

9

You are probably using the wrong user. Check if PHP uses the same user that owns the directory you are trying to write. PHP often uses www-data (www-data is the user that web servers on Ubuntu use such as Apache and Nginx for example)) so if the directory is chmodded to 755 it means that the user created the directory can write to it, but others can only read. chown to the php user or chmod to 777.

I personally run PHP fastcgi, it runs with an unique user so I don't have this problem, think about switching to fastcgi.

Marc
  • 5,109
  • 2
  • 32
  • 41
Adam Arold
  • 29,285
  • 22
  • 112
  • 207
  • This applies to the file test.progress as well. If the file already exists, make sure it's also writable by the PHP user. – Emil Vikström May 13 '11 at 09:56
  • Mine was this issue! - mamp pro, apache/web etc was running under www/mysql - instead of name/name :) – VeenarM Jul 13 '13 at 23:39