Cron jobs containing relative paths fail

Started by dg1, September 18, 2017, 01:18:39 PM

Previous topic - Next topic

dg1

It seems, if a PHP script contains something like:

include('../another-file.php');

It will work properly if opened in browser or CLI, but fail in cron with a message similar to:


PHP Warning:
include(../another-file.php): failed to open stream: No such file or directory in /home/account/public_html/cron-test.php on line 3


Scripts can be modified to use absolute paths instead of relative paths, but that's not the best solution for everyone, especially if lots of files are executed using cron jobs.

Is there a fix for this at the control panel (CWP) level?

dg1

An example of a fix (not tested thoroughly) could be to include this line in the beginning of each script that is executed via cron.

chdir(__DIR__);

Still, would be nice to see a fix on CWP level.

Sandeep

#2
use :
include($_SERVER["DOCUMENT_ROOT"] . "patch to another-file.php");

or

include(dirname(__FILE__) . "path to another-file.php");

dg1

OK, this works for files in the same folder by supplying the absolute URL, but what about relative paths?

What's the replacement for ('../abc.php')?

How about ('../../abc.php')?

Or ('./abc.php')?