Problems writing to file with PHP on Ubuntu

I am running PHP 5.5.9 on Ubuntu 14.04. I am having trouble writing to a file. I feel this is a file permissions issue because I'm sure the code is correct. The user I signed up with has the rights to write to the folders I'm trying to write, but I'm not sure what localhost does. I am not sure what the localhost username is for using chmod . I tried using chmod 777 -R /var/www/html and the script still could not write to my target folder which has the path /var/www/html/Projects/MD_ScrapingTool/files . Here is my code:

 $file = 'filetest.txt'; if($handle = fopen($file, 'w')) { $content = "123\n456"; fwrite($handle, $content); fclose($handle); } else { echo "Could not open file for writing."; } 
+8
php file-io ubuntu apache file-permissions
source share
3 answers

After some research, I get it. Here is a solution for those who have the same problem:

Since localhost belongs to the www-data group, I just added my user to this group.

 sudo usermod -a -G www-data my_username 

Then I added the folder to the group.

 sudo chgrp -R www-data /var/www 

Then I gave permission to write to the www-data group.

 sudo chmod -R g+w /var/www 

This worked for me without any other problems. Thanks!

+16
source share

Try specifying the absolute path and file name with open :

 $file = '/html/Projects/MD_ScrapingTool/files/filetest.txt'; 

If the file does not exist, fopen should create a file with 'w' .

0
source share

You can make sure that you have permission to the folder "/ html / Projects / MD_ScrapingTool / files /" and try with the absolute path.

0
source share

All Articles