Enabled PHP functions: fopen, fclose, fwrite

July 17, 2016 485 views
LAMP Stack PHP Configuration Management DigitalOcean Ubuntu

I just setup my first Ubuntu server and things were going smoothly until I realized that my tiny php script:

    // write email to a file
    if (isset($_POST['email'])) {
        $myfile = fopen("emails.txt", "a") or die("Unable to open file!");
        $txt = $_POST['email']."\n";
        fwrite($myfile, $txt);

was no longer working... I poked around the help boards, but couldn't find any articles that were hand-holdy enough to get me a solution. Wondering if anyone can help!

1 Answer
MDS July 17, 2016
Accepted Answer

Congratulations on your first server!

Try setting ownership of the emails.txt file by running the commands below. I'm assuming your php and emails.txt files are located in /var/www/html folder, if not you can change the path of the command below.

chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html
  • That did it, thank you so much!!

  • Ah crud, is there any way to maintain ssh (sftp) write permissions after doing this?

    • chmod -R 766 /var/www/html

      This will allow other groups to read/write files in /var/www/html

Have another answer? Share your knowledge.