PHP:需要路径不适用于 cron 作业?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3118699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: Require path does not work for cron job?
提问by gsquare567
I have a cron job that needs to include this file:
我有一个需要包含此文件的 cron 作业:
require '../includes/common.php';
however, when it is run via the cron job (and not my local testing), the relative path does not work. the cron job runs the following file (on the live server):
但是,当它通过 cron 作业(而不是我的本地测试)运行时,相对路径不起作用。cron 作业运行以下文件(在实时服务器上):
/home/username123/public_html/cron/mycronjob.php
and here's the error:
这是错误:
Fatal error: require(): Failed opening required '../includes/common.php'
(include_path='.:/usr/lib/php:/usr/local/lib/php') in
/home/username123/public_html/cron/mycronjob.php on line 2
using the same absolute format as the cron job, common.phpwould be located at
使用与 cron 作业相同的绝对格式,common.php将位于
/home/username123/public_html/includes/common.php
does that mean i have to replace my line 2 with:
这是否意味着我必须用以下内容替换我的第 2 行:
require '/home/username123/public_html/includes/common.php';
?
?
thanks!
谢谢!
回答by Robus
Technically seen the php script is run where cron is located; ex. If cron was in /bin/cron, then this statement would look for common.php in /bin/includes/common.php.
技术上看php脚本是在cron所在的地方运行的;前任。如果 cron 在 /bin/cron 中,那么该语句将在 /bin/includes/common.php 中查找 common.php。
So yeah, you'll probably have to use fullpaths or use set_include_path
所以是的,您可能必须使用完整路径或使用set_include_path
set_include_path('/home/username123/public_html/includes/');
require 'common.php';
回答by Gabriel Sosa
nono. you need to use absolute paths on crons.
不,不。您需要在 cron 上使用绝对路径。
what I do is:
我做的是:
// supouse your cron is on app/cron and your lib is on app/lib
$base = dirname(dirname(__FILE__)); // now $base contains "app"
include_once $base . '/lib/db.inc';
// move on
回答by Accountant ?
With all do respect to all the current answers, they all went to "change the php code" approach.
所有人都尊重所有当前的答案,他们都采用了“更改 php 代码”的方法。
I don't like to change my PHP files just to run it from a cronbecause it reduces the code portability and increases the chances to forget to change one or two relative paths and break the program.
我不喜欢仅仅为了从 a 运行它而更改我的 PHP 文件,cron因为它降低了代码的可移植性并增加了忘记更改一两个相对路径并破坏程序的机会。
Instead change the directory at the crontab line, and leave all your relative paths and your PHP files untouched.For example
而是在cron选项卡行更改目录,并保持所有相对路径和 PHP 文件不变。例如
1 1 * * * cd /home/username/public_html/&& php -f script.php
check this answer
检查这个答案
also check this article, I will quote the relative part
也检查这篇文章,我会引用相关的部分
Depending on the code in your PHP script, it may only run correctly when called from a specific directory. For example, if the script uses relative paths to include files, it will only run if it is called from the correct directory. The following command shows how to call a PHP script from a specific directory:
根据 PHP 脚本中的代码,它可能只有在从特定目录调用时才能正确运行。例如,如果脚本使用相对路径来包含文件,则只有从正确的目录调用它才会运行。以下命令显示了如何从特定目录调用 PHP 脚本:
cd /home/username/public_html/; php -q script.php
回答by Scott C Wilson
An alternative to the solutions which recommend absolute path specification is using a chdirin your script. That way, your relative paths will work as expected.
推荐绝对路径规范的解决方案的替代方法是chdir在脚本中使用 a 。这样,您的相对路径将按预期工作。
For example, to change to the directory of the script:
例如,要更改到脚本的目录:
$curr_dir = dirname(__FILE__);
chdir($curr_dir);
To change to the parent directory of the script:
要更改到脚本的父目录:
$curr_dir = dirname(__FILE__);
chdir($curr_dir . "/..");
And so forth.
等等。
回答by kiamlaluno
If the relative path doesn't work, then it means that the current directory set when the cron tasks are running is not /home/username123/public_html. In such cases, you can only use an absolute path.
如果相对路径不起作用,则表示cron任务运行时设置的当前目录不是/home/username123/public_html。在这种情况下,您只能使用绝对路径。
回答by MANCHUCK
It sounds as simple as just some script you are running is setting the include_path and you are including that script. use phpinfo() to check the include_path global vs local setting.
这听起来很简单,因为您正在运行的一些脚本正在设置 include_path 并且您正在包含该脚本。使用 phpinfo() 检查 include_path 全局与本地设置。

