LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP file will not open because of wrong permissions - in firefox browser (https://www.linuxquestions.org/questions/programming-9/php-file-will-not-open-because-of-wrong-permissions-in-firefox-browser-4175694711/)

pizzipie 05-04-2021 03:06 PM

PHP file will not open because of wrong permissions - in firefox browser
 
Despite several manipulations of owner, group, and other permissions I can't get php to open/create a file.

Here is a complete test script to show what is going on.

Code:

<!DOCTYPE HTML>

<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html"; charset="UTF-8" />     
    <title> OptumRx Log 14Aug2020, 28Sep2020, 04May2021</title>
 
</head>
<body>

<div class="header">

        <h1>Test script to open - optumRx.php - file</h1>
</div> <!--header-->

<div class="container">

<?php // =================== PHP  ============================

error_reporting (E_ALL ^ E_NOTICE);
//include('../include/myPhpFunctions.inc');


$log='optumRx.log';
$timezone="America/Los_Angeles";  // see mytimestamp() in myPhpFunctions.inc
$format="D M d, Y";
$incTime=true;

echo mytimestamp($timezone);
echo "<br><br>user ... ".exec(whoami)."<br>";
echo "<br>File permissions: <br>";
echo "-rwxrwxr-- 1 www-data rick 1434 May  4 12:04 insertQuery.php<br><br>";
echo "drwxrwsr-x  7 rick rick  4096 May  3 16:51  DBases OR<br>";
echo "drwsrwsr-x  7 rick www-data  4096 May  3 16:51  DBases OR<br>";
echo "drwsrwsr-x  7 www-data rick  4096 May  3 16:51  DBases<br><br>";
echo "drwxrwsr-x 14 rick rick  4096 May  3 13:26 Dbmysql<br><br>";

echo "APACHE2 error.log:<br>";
echo "PHP Warning:  fopen(optumRx.log): failed to open stream:";
echo "Permission denied in /home/rick/DBases/Dbmysql/optumRx/insertQuery.php on line 36<br><br>";
 


echo '$fh=fopen($log, "a+") says ...<br>';

                  if(!($fh=fopen($log, "a+"))) die("CAN NOT OPEN LOG FILE"); // log database insert operations
            $permission=substr(sprintf("%o", fileperms($log)),-4)."\n";
            echo "permission ... ".$permission;
                                if($permission !="0660") {
                                        chmod($log,0660);
                                        chown($log, "www-data:rick");
                                        clearstatcache();
                                }                       
            fwrite($fh, "# ".mytimestamp($timezone)."\n\n"); // see line 9
                fwrite($fh, $query."\n\n");                  // write insert to log
                                fwrite($fh, "# ========================================================\n");
                                fclose($fh);
       
                       


function mytimestamp($tzone) {
        date_default_timezone_set($tzone);
        return date('D, M d, Y H:i:s');
        }

?>  <!-- =================  HTML  ============================ -->

</body>
</html>

RESULT:
Quote:

Test script to open - optumRx.php - file
Tue, May 04, 2021 12:54:49

user ... www-data

File permissions:
-rwxrwxr-- 1 www-data rick 1434 May 4 12:04 insertQuery.php

drwxrwsr-x 7 rick rick 4096 May 3 16:51 DBases OR
drwsrwsr-x 7 rick www-data 4096 May 3 16:51 DBases OR
drwsrwsr-x 7 www-data rick 4096 May 3 16:51 DBases

drwxrwsr-x 14 rick rick 4096 May 3 13:26 Dbmysql

APACHE2 error.log:
PHP Warning: fopen(optumRx.log): failed to open stream:Permission denied in /home/rick/DBases/Dbmysql/optumRx/insertQuery.php on line 36

$fh=fopen($log, "a+") says ...
CAN NOT OPEN LOG FILE
Hope someone can steer me in the right direction to solve this as it is very frustrating!

astrogeek 05-04-2021 07:25 PM

Probably this...

Code:

drwxrwsr-x 14 rick rick  4096 May  3 13:26 Dbmysql

PHP Warning: fopen(optumRx.log): failed to open stream:Permission denied in /home/rick/DBases/Dbmysql/optumRx/insertQuery.php on line 36


pizzipie 05-04-2021 07:53 PM

that is what I have now.

uteck 05-04-2021 08:08 PM

You have permissions set for DBases, but what about the files and directories under it? www-data may not be able to see anything else.

astrogeek 05-04-2021 09:26 PM

Quote:

Originally Posted by pizzipie (Post 6248068)
that is what I have now.

Yes, but my point is that www-data cannot write to a directory with those permissions...

If the apache/php processes need to write down that path, perhaps:

Code:

chown -R rick:www-data DBases/

pizzipie 05-08-2021 12:04 PM

I found this site and followed the suggestions - now works.

https://appuals.com/how-to-fix-forbi...ing-up-apache/

Thanks for your help!

R


All times are GMT -5. The time now is 01:44 PM.