john murray
john murray

Reputation: 49

PHP apache2 - php script cli will write txt file but browser will not

I'm running an Apache2 server (lamp) with PHP8.2 on Raspberry pi5. I have transferred all my code from a previous Pi4b. but installed the lamp server afresh. I have some simple php script files which receive data via args intended to be used from a browser. At its simplest this works when invoked in a command line but when invoked via a browser seems to work but doesnt change the file (i.e. it says it has written 40+bytes but theres no change in file if the script in invoked through a browser. There are quite a number of threads referring to similar situations - spent last 6 hours reading. Obviously file and directory permissions are first investigation - the directory referred to is chowned by www-data and chmod to 777

error_reporting(E_ALL);

echo  exec('whoami');
echo "<br>";
if (PHP_SAPI === 'cli') {
    if (count($argv)>2)
    {
        $ml = $argv[1];
        $orp=$argv[2];
        echo 'command line inv <br>';
    }else
    {
        $ml=-1;
        $orp=-1;
        echo 'command line invoke without req params so read old file';
        $tmp= @file_get_contents('/var/tmp/phorp.txt');
        if ($tmp!=false)
        {
            parse_str($tmp,$out);
            $ml=$out['phval'];
            $orp=$out['orpval'];
        }else
        {$ml=0;
            $orp=999;
        }
    }
}
else {
    if (empty($_GET)) { 
        $tmp= file_get_contents('/var/tmp/phorp.txt');
        if ($tmp!=false)
        {
            parse_str($tmp,$out);
            $ml=$out['phval'];
            $orp=$out['orpval'];
        }else
        {
            $ml=0;
            $orp=99;
        }
    }   // else this is being invoked in browser
    else
    {
        $ml = $_GET['arg1'];
        $orp=$_GET['arg2'];
        echo "via browser  -ph=".$ml."   orp=".$orp;
        echo '<br>';
    }
}
$datenow = date('d/m/Y H:i');
echo $datenow;
//unlink("/var/tmp/phorp.txt");
$myfile = fopen("/var/tmp/phorp.txt", "wb") or die("Unable to open file!");
$res =fwrite($myfile, "phval=".$ml.'&'."orpval=".$orp."&date=".$datenow);
fclose($myfile);

echo  "bytes written = ".$res;
echo "<br>";
echo 'got here  seems to write<br>';

so as Ive said that code works in cli but when called from a browser does not. Obviously when called cli the user is root or main admin user but the Apache2 user is www-data and I've given ownership and mod'ed the directories and files appropriately (viz- /var/tmp is owned by www-data and has 777 permissions I have found several strange things happen with my new Pi5 and the latest 'bookworm' (e.g. onewire set up grrr!) but this should be straightforward. I've looked at php.ini on the old Pi4 server which worked but i cant see anything different in the new and old php.ini Can anyone suggest what is happening here? Should I post PHP.ini if so what sections?

For info and this is what I think is weird behaviour - when called in browser the 'echo' lines at bottom of file report 42 bytes written - no errors thrown up but the phorp.txt is NOT changed

Upvotes: 1

Views: 35

Answers (0)

Related Questions