I'm trying to write some command line PHP - is there a trick to writing to a file from the command line using PHP? The file is called testcl.php and I execute it with:
/usr/bin/php -f /var/www/html/media/testcli.php
$myFile = "test.txt";
$fh = fopen($myFile, 'a');
fwrite($fh, "batman2");
fclose($fh);
echo "batman";
The batman echo statement works, so I know the file is running - but it's not writing to test.txt for some reason. If I access testcli.php it writes text.txt successfully - so I think I'm missing some trick.
I suspect you have a CWD issue: test.txt is a relative path, so it will be calculated for wherever you stand while calling the script - this might be differet between you on the shell an the webserver. Try
$myFile=dirname(__FILE__).'/test.txt';
It might also be a permission problem: Make sure, you have write privileges for the websrver directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With