I am trying to save data using HTML and PHP in localhost in Ubuntu. My HTML & PHP files are working great but when I am using commands to open a file in the localhost using

$myfile = fopen("testfile.txt", "w")

it does not allow it to create it, but the PHP file is working and when set with commands to output variables from PHP it works well.

How to permit PHP to create files in Apache in localhost Ubuntu.

  • Does the user running the web server have write privileges on this file (if existing) or on the containing folder (if creating a new file)? That user also needs directory traversal privileges on all recursively containing directories. Commented May 24, 2015 at 8:54
  • My question refers to that only i believe that - how do I set my apache2 to allow writing new files that php creates? Commented May 24, 2015 at 10:39
  • It's not just a question of Apache configuration, but also of file system permissions, since you want Apache to write in the file system. Commented May 24, 2015 at 12:50
  • ok, i am having issues with usage of this forum, I mean why am i getting voted down for a question that i find relevant with Ubuntu. My problem is solved but still I am being prompted for getting low quality response for my question. Commented May 31, 2015 at 17:05
  • 1
    See How do I ask a good question? and What topics can I ask about here? for possible reasons. People are currently voting this as off topic, which I can only explain plausibly, because it may be appear to be about PHP programming and not Ubuntu web server administration on first sight. The downvotes may result from the lack of necessary, requested information in your question to track down the cause of the problem. If you want to know more, open a question on Meta.AU. Commented May 31, 2015 at 20:14

5 Answers 5


You must allow the Apache user (usually www-data) to create files in that directory. This can be accomplished by making Apache the owner of the directory, for example, if the directory were called my-dir, you would open a terminal and run this:

sudo chown www-data my-dir

as stated on the Apache2 Ubuntu Default Page: By default, Ubuntu does not allow access through the web browser to any file apart of those located in /var/www .... This means that any directory or file you want to write to must be under /var/www. Missed this info myself and got my hair pulling for a while trying to write to /tmp etc.


If you want to change all the folders and files within that directory, and not just the directory, add -R like so

sudo chown -R www-data my-dir

If your user is daemon, then you need to ensure that the daemon user has appropriate permissions to access the directory /opt/lampp/htdocs/classes/php where you want to create files.

Here's how you can give daemon user write permission to the directory:

sudo chown -R daemon:daemon /opt/lampp/htdocs/classes/php

This command recursively changes the ownership of the directory and its contents to the daemon user and group. After changing ownership, you can then modify the permissions to allow write access:

sudo chmod -R u+w /opt/lampp/htdocs/classes/php

This command gives the owner (daemon user) write permission to the directory and its contents.

With these commands, the daemon user should be able to create files in the /opt/lampp/htdocs/classes/php directory. Make sure to replace daemon with the appropriate user if it's different on your system.

and check screenshot


If you are creating the File on the fly and you do not want to save it on the server but instead download it on client computer after an action is triggered from client side e.g. like a button click from a User Web Browser, you can write the PHP code as follows;


$dataToWriteToFile = "This Text has been saved in this Text File and Downloaded for you to View !";
$fileName = "TxtFilename.txt";

$file = fopen($fileName,"wb");
fwrite($file, $dataToWriteToFile);

header('Content-Type: charset=utf-8');
header("Content-disposition: attachment; filename=$fileName");

print $dataToWriteToFile;


DISCLAIMER: It will not work for those intending to write a file to server for archiving but for anyone else creating a file on the fly for purpose of making their client users download it to their web browsers it may suffice for that purpose.

I was about to talk to my client to request them to allow the Apache user to add more rights to the Apache user (as is suggested by previous answers here and other related Questions on internet) to allow writing files to the folder where their server web pages are served from. I am almost certain this would have been an uphill task for them to agree to but luckily the above code worked for me.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .