Enabled PHP functions: fopen, fclose, fwrite

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

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

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.