I have a php script, that contains some text file writing operation. Now if I execute the script from browser, by accessing it using it’s url, it works fine and file is written perfectly. But when I run the same script from terminal, using command, the script runs but no file operation is performed. I am not sure, why this happens. I know it may be a silly thing or I am missing some key or anything but can’t able to figure it out as of now.
Here is the command I am using
/path/to/php -f /path/to/file
Here is the code which writes to text file:
$myFile = "filename.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
$str = "file content here";
fwrite($fh, $str);
fclose($fh);
Thanks for the help.
This may be using different paths. For example, when run from the terminal it will be looking for “filename.txt” in the directory you are in when you run the command, whereas when running from the browser it will look in the same directory as your PHP file.
To fix this use the full path rather than just the filename for the file you are accessing.