开发者

Does it matter if I use relative or absolute path's when running a php script via cron?

开发者 https://www.devze.com 2023-02-09 15:30 出处:网络
I am currently working on a script that I am attempting to automate via cron. Running the script via terminal is just fine, but when I attempt to run the script with crontab, I am getting some issues.

I am currently working on a script that I am attempting to automate via cron. Running the script via terminal is just fine, but when I attempt to run the script with crontab, I am getting some issues.

Part of my script loads and validates and xml file via DOMDocument::loadXML() and DOMDocument::validate() and php throws an error when attempting to validate stating:

Failed to load external entity: /linuxuser/homefolder/开发者_C百科my_dtd.dtd

Within the xml file, the dtd is set to:

../../../../../../../my_dtd.dtd

Is there some misconfiguration of the server or is it more likely something wrong with my php code at this point? It seems to grab my linux home directory rather than the path relative to the xml file. Just wondering if anyone else has seen an issue like this or could point me in the right direction. Thanks.


Quoting the PHP documentation for differences in CLI usage (command-line interface):

The CLI SAPI does not change the current directory to the directory of the executed script!

When PHP scripts are run via CRON, it is executed on the user's home directory. You can either replace all relative path references used by the script to absolute, or place this on the start of the script:

chdir(dirname(__FILE__)); # for PHP 5.2.x and below
# or
chdir(__DIR__); # for PHP 5.3+


Most likely, the problem is in working dir and resolving relative path.


Try absolute path in your xml file.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号