2

I am running Ubuntu version 14.04, and I have an apache2 server set up to host some php files on localhost that are stored in /var/www/html.

When I try to execute the following code in one of my php script

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

I do not find a file stored in my /var/www/html named test.txt, or anywhere on my computer after searching. The file name does not already exist, and I am using sudo nautilus because I do not have the necessary permissions to edit the php files inside the /var/www/html. Why are the files not appearing in my /var/www/html folder and how can I fix this so that the file is created?

  • User www-data having write access to the www directory (or one of its subfolders) is a major security hazard on production, and bad practice in general. Please pick a separate data folder, or use a database engine instead. – Ruud Helderman Nov 02 '14 at 17:26

1 Answers1

5

I just tested your php file in my server and it works just fine.

You problem must be that the user www-data doesn't have write permissions to that folder.

If indeed that user doesn't have write permissions to that folder, then you will see error messages at the file /var/log/apache2/error.log

In order to look at the last errors, do

tail -n 15 /var/log/apache2/error.log

For example, if I, instead of "test.txt", put "/test.txt" (write to /, were only root can) I get this error inside the log file:

[:error] [pid 7505] [client 127.0.0.1:47066] PHP Warning:  fopen(/test.txt): failed to open stream: Permission denied in /var/www/html/a.php on line 2

The above clearly states that permission is denied.

You can fix this problem by giving write permission to that folder for user www-data.

give specific user permission to write to a folder using +w notation

hytromo
  • 4,904
  • I appreciate your effort to help, but when I gave the file permissions it did not work. I also did not find the line you gave in my error log. – John Lee Nov 02 '14 at 14:14
  • Can you please go from your browser to the php page you posted and then edit your post with the contents of the error.log file? – hytromo Nov 02 '14 at 14:30
  • Never mind, I found the problem. It was in the error log, I was just searching the wrong one. Thank you. – John Lee Nov 02 '14 at 15:56