Click here to Skip to main content
14,930,087 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.
HTML



<!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:

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 


What I have tried:

see above code ??????????????????????????????????????????????????????????????????
Posted
Updated 8-Jun-21 7:18am
Comments
Richard MacCutchan 5-May-21 16:23pm
   
You need to capture the actual error code when the fopen call fails.
Gerry Schmitz 5-May-21 17:09pm
   
If it's actually a "log" file, maybe it's in use by the "logger"; you're trying to open it for writing.

1 solution

Your apache server probably doesn't run as rick. Have you tried changing the file location to somewhere that your apache process has write permissions?
ps -ef | grep http

should tell you which user owns the apache process.

The directories where your are trying to open the file don't have world write set, so if you and the apache process don't share a group, it won't be able to write there. The groups command will tell you what groups you (and the apache owner) are in.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900