4

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. – David Foerster May 24 '15 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? – Moithil Biswas May 24 '15 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. – David Foerster May 24 '15 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. – Moithil Biswas May 31 '15 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. – David Foerster May 31 '15 at 20:14
6

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
| improve this answer | |
2

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
| improve this answer | |
0

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.

| improve this answer | |
0

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;

<?php 

$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);
fclose($file);

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.