相对路径在cron PHP脚本中不工作 [英] Relative path not working in cron PHP script

查看:114
本文介绍了相对路径在cron PHP脚本中不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果PHP脚本作为cron脚本运行,则如果使用相对路径,则include脚本通常会失败。例如,如果您有

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脚本中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆