[英]Relative path not working in cron PHP script
如果 PHP 脚本作为 cron 脚本运行,则在使用相对路径时包含通常会失败。 例如,如果你有
require_once('foo.php');
在命令行上运行时会找到文件 foo.php,但在从 cron 脚本运行时不会找到。
一个典型的解决方法是首先 chdir 到工作目录,或者使用绝对路径。 但是,我想知道导致这种行为的 cron 和 shell 之间有什么不同。 为什么在 cron 脚本中使用相对路径会失败?
将工作目录更改为运行文件路径。 只需使用
chdir(dirname(__FILE__));
include_once '../your_file_name.php'; //we can use relative path after changing directory
在运行文件中。 那么您就不需要将每个页面中的所有相对路径都更改为绝对路径。
从 cron 运行时,脚本的工作目录可能不同。 此外,关于 PHP 的 require() 和 include() 存在一些混淆,这导致了对工作目录真正是问题的混淆:
include('foo.php') // searches for foo.php in the same directory as the current script
include('./foo.php') // searches for foo.php in the current working directory
include('foo/bar.php') // searches for foo/bar.php, relative to the directory of the current script
include('../bar.php') // searches for bar.php, in the parent directory of the current working directory
我获得“require_once”同时使用 cron 和 apache 的唯一机会是
require_once(dirname(__FILE__) . '/../setup.php');
因为 cron 作业的“当前工作目录”将是您的 crontab 文件所在的目录——因此任何相对路径都相对于该目录。
最简单的处理方法是使用dirname()
函数和 PHP __FILE__
常量。 否则,每当您将文件移动到不同目录或具有不同文件结构的服务器时,您都需要使用新的绝对路径编辑文件。
dirname( __FILE__ )
__FILE__
是一个常量,由 PHP 定义为调用它的文件的完整路径。 即使包含文件, __FILE__
也将始终引用文件本身的完整路径——而不是执行包含的文件。
所以dirname( __FILE__ )
返回包含文件的目录的完整目录路径——无论它包含在何处,而basename( __FILE__ )
返回文件名本身。
例如:假设“/home/user/public_html/index.php”包含“/home/user/public_html/your_directory/your_php_file.php”。
如果您在“your_php_file.php”中调用dirname( __FILE__ )
,即使活动脚本位于“/home/user/public_html”中,也会返回“/home/user/public_html/your_directory”(注意没有尾部斜杠)。
如果您需要包含文件的目录,请使用: dirname( $_SERVER['PHP_SELF'] )
它将返回“/home/user/public_html”并且与在“index.php”文件中调用dirname( __FILE__ )
相同因为相对路径是一样的。
示例用法:
@include dirname( __FILE__ ) . '/your_include_directory/your_include_file.php';
@require dirname( __FILE__ ) . '/../your_include_directory/your_include_file.php';
另一种可能性是 CLI 版本使用的是不同的 php.ini 文件。 (默认情况下,它将使用 php-cli.ini 并回退到标准的 php.ini)
此外,如果您使用 .htaccess 文件来设置库路径等,这显然无法通过 cli 工作。
除了上面接受的答案外,您还可以使用:
chdir(__DIR__);
当通过 cron 作业执行时,您的 PHP 脚本可能会在与您从 shell 手动启动不同的上下文中运行。 所以你的相对路径没有指向正确的路径。
尽管 DIR 无法在我的本地主机上工作,但它可以工作,因为它的路径与我的实时站点服务器不同。 我用这个来修复它。
if(__DIR__ != '/home/absolute/path/to/current/directory'){ // path for your live server
require_once '/relative/path/to/file';
}else{
require_once '/absolute/path/to/file';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.