Able to Read but not Write to a file using PHP/Apache on CentOS

December 27, 2019 421 views
CentOS Apache PHP

$myfile = fopen(“testData.txt”, “r”) or die(“Unable to open file!”);
echo fread($myfile,filesize(“testData.txt”));
fclose($myfile);

Above works! The following fails using either “a”-append or “w”-write!

$myfile = fopen(“testData.txt”, “a”) or die(“Unable to open file!”);
$txt = “John Doe\n”;
fwrite($myfile, $txt);
$txt = “Jane Doe\n”;
fwrite($myfile, $txt);
fclose($myfile);

ls -l returns:
-rw-rw-rw-. 1 user user 24 Dec24 07:54 testData.txt

I am sure that it is a permissions issue somewhere, but no clue where to look. PHP file and the data file are in the same directory (I know this is not good practice - just trying to eliminate as many variables as possible).

Thank you in advance.

1 comment
2 Answers

Hi @revans2695,

It’s possible you’ve or any other system has made the file immutable. Please execute the following command on the file

lsattr testData.txt

It will show you the attributes of the file. Example

 lsattr test
----i---------e--- test

The above example shows the file has a tag of -i and -e. The -i tag means immutable while the -e tag means executable. To remove the -i tag, you would need to do

chattr -i test

Regards,
KDSys

  • Thank you KDSys for the quick response.

    However, the lsattr command returns all ———————.

    • Hi @revans2695,

      I see, in that case try with a more permissive permissions to see if it would actually resolve the issue.

      If making the file executable like so

      chmod +x nameOfFile
      

      works afterwards than it’s 100% permissions issue. If not we’ll continue from there

      Regards,
      KDSys

      • Thank you for staying with me on this. I executed the command recommended above.

        The command: ls /var/www/html/xxxxxxxx/cronTestData.txt results
        -rwxrwxrwx. 1 userx userx 24 Dec 24 07:54

        I still get “Unable to open file!”

        • Hi @revans2695,

          This is getting really tricky. If you try and edit the file from the terminal you are not presented with any errors, correct?

          • That is correct. I used the vi editor to edit the file and append a new line at the end of the file.

            The failure is occurring when I run the PHP file from a browser. Could it be a permissions issue somewhere because Apache is in a different group than the group the owner of the file is in.... even though the permissions on the file are set to -rwxrwxrwx. Here is the script:

            <?php

            echo (“Read ”);
            $myfile = fopen(“cronTestData.txt”, “r”) or die(“Read - Unable to open file!\n”);
            echo fread($myfile,filesize(“cronTestData.txt”));
            fclose($myfile);

            echo (“Write\n”);

            $myfile = fopen(“cronTestData.txt”, “a”) or die(“Write - Unable to open file!”);
            $txt = “John Doe\n”;
            fwrite($myfile, $txt);
            $txt = “Jane Doe\n”;
            fwrite($myfile, $txt);
            fclose($myfile);
            echo (“Write Success!\n”);

            ?>

            The read works fine and lists the four lines in the file. The write fails with: Write Write - Unable to open file!

            Thanks for your help on this. It is quite frustrating.

Hi @revans2695,

It does seem exactly like that, if you are able to open it with the user from the shell and not from the browser, I would say exactly that.

Regards,
KDSys

Have another answer? Share your knowledge.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!