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.txtis 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. TryIt might also be a permission problem: Make sure, you have write privileges for the websrver directory.