Permission denied when opening or creating files using PHP

Possible duplicate:
mysql_fetch_array () expects parameter 1 to be a resource, boolean is set to select

I can not create files with php, because for this permission was granted to access the file. I get this error:

Warning: fopen (test.txt): Could not open stream: permission denied in /web/com/example.com/index.php on line 20
Warning: fwrite () expects parameter 1 to be a resource, boolean is listed in /web/com/example.com/index.php on line 21
Warning: fclose () expects parameter 1 to be a resource, boolean is listed in /web/com/example.com/index.php on line 22

This is the code I used:

<?php $file = fopen("test.txt","w"); echo fwrite($file,"Hello World. Testing!"); fclose($file); ?> 

Just! This is sample code from w3schools .

Therefore, I need help to learn how to give the file the necessary permissions. I upload files to my site using net2ftp FTP application, if that matters.

+4
source share
2 answers

The folder that your PHP script is trying to write will probably be owned by the root user. Your PHP script is more than likely running under the www-data user if you use the default Ubuntu / Apache / PHP setting.

As such you need:

 chown -R www-data:www-data folder chmod -R g+w folder 

If you find that PHP runs as a user other than www-data, just change the user to a group in the first line of code.

PS. change the "folder" for your actual folder name.

+13
source

The user running PHP (usually the apache user) is not allowed to write to the folder in which the script is running. Try using an absolute path, for example, "/tmp/test.txt" - tmp is usually writable by any user, but the contents are usually destroyed upon reboot.

+3
source

Source: https://habr.com/ru/post/1395482/


All Articles