Relative path not working in cron PHP script(相对路径在 cron PHP 脚本中不起作用)
问题描述
如果 PHP 脚本作为 cron 脚本运行,如果使用相对路径,则包含通常会失败.例如,如果您有
If a PHP script is run as a cron script, the includes often fail if relative paths are used. For example, if you have
require_once('foo.php');
在命令行上运行时会找到文件 foo.php,但在从 cron 脚本运行时不会找到.
the file foo.php will be found when run on the command line, but not when run from a cron script.
对此的典型解决方法是先将 chdir 指向工作目录,或使用绝对路径.但是,我想知道导致这种行为的 cron 和 shell 之间有什么不同.为什么在 cron 脚本中使用相对路径会失败?
A typical workaround for this is to first chdir to the working directory, or use absolute paths. I would like to know, however, what is different between cron and shell that causes this behavior. Why does it fail when using relative paths in a cron script?
推荐答案
从 cron 运行时,脚本的工作目录可能不同.此外,关于 PHP 的 require() 和 include() 存在一些混淆,这导致了工作目录真正是问题所在的混淆:
The working directory of the script may be different when run from a cron. Additionaly, there was some confusion about PHPs require() and include(), which caused confusion about the working directory really being the problem:
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
这篇关于相对路径在 cron PHP 脚本中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:相对路径在 cron PHP 脚本中不起作用


- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01