The issue I am having involves the use of fopen and fwrite to write to a text file on the server. When I call my function from within a browser, the file is opened and written to as expected. When I call the function from a scheduled task running on the server, the file is never written to though no error is ever thrown. I don't believe it to be a permissions issue but I'm not sure what else it could be. Has anyone else experienced something like this?
See the following snippet of code I'm using:
$file = "../logs/" . $token . ".txt";
$message = "Test Message";
fwrite(fopen($file, 'a+'), $message);
When a script is executed by cron (I assume you mean cron with scheduling tasks) then the working directory of script will be the file system root /
. Workaround: Use absolute paths in your script:
$file = "/path/to/logs/" . $token . ".txt";
$message = "Test Message";
fwrite(fopen($file, 'a+'), $message);
or use the __DIR__
constant to build paths relative to the script source file:
$file = __DIR__ . "../logs/" . $token . ".txt";
$message = "Test Message";
fwrite(fopen($file, 'a+'), $message);