zaterdag 19 maart 2011

[SOLVED] PHP cannot write to file

When trying to execute a PHP script that should write text content to a file, I ran into trouble.
First of all, what did I do?

$myfile = '/var/www/dev/log/mylog.log';
$fh = fopen($myfile, 'w');
fwrite($fh, "add some content\n");
fclose($fh);


Then, when executing and nothing happens: I did the following:

  • set log directory to writable: 777

But again nothing worked...
Finally I found out that I had to set the owner of the directory.

By typing 'ps aux | grep apache' into the terminal I found out that apache is runned with the user www-data.

Now, when the log folder is owned by www-data user, it becomes accessible for apache. To do this, type: chown www-data /var/www/dev/log/ (or any other folder you want to use). Also make sure the folder is 777 and maybe you need to delete previous logfiles in order to let PHP create new ones.

Geen opmerkingen: