简体   繁体   中英

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

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/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+

尝试xml文件中的绝对路径。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM