相对路径在 cron PHP 脚本中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1969374/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:29:30  来源:igfitidea点击:

Relative path not working in cron PHP script

phpcron

提问by Sjoerd

If a PHP script is run as a cron script, the includes often fail if relative paths are used. For example, if you have

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

require_once('foo.php');

the file foo.php will be found when run on the command line, but not when run from a cron script.

在命令行上运行时会找到文件 foo.php,但在从 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?

一个典型的解决方法是首先 chdir 到工作目录,或者使用绝对路径。但是,我想知道导致这种行为的 cron 和 shell 之间有什么不同。为什么在 cron 脚本中使用相对路径会失败?

采纳答案by Sjoerd

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:

从 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

回答by Withfriendship Hiox

Change the working directory to the running file path. Just use

将工作目录更改为运行文件路径。只需使用

chdir(dirname(__FILE__));
include_once '../your_file_name.php'; //we can use relative path after changing directory

in the running file. Then you won't need to change all the relative paths to absolute paths in every page.

在运行文件中。那么您就不需要将每个页面中的所有相对路径都更改为绝对路径。

回答by jansch

The only chance I got "require_once" to work with cron and apache at the same time was

我获得“require_once”同时使用 cron 和 apache 的唯一机会是

require_once(dirname(__FILE__) . '/../setup.php');

回答by aequalsb

Because the "current working directory" for cron jobs will be the directory where your crontab file exists -- so any relative paths with be relative to THAT directory.

因为 cron 作业的“当前工作目录”将是您的 crontab 文件所在的目录——因此任何相对路径都相对于该目录。

The simplest way to handle that is with dirname()function and PHP __FILE__constant. Otherwise, you will need to edit the file with new absolute paths whenever you move the file to a different directory or a server with a different file structure.

最简单的处理方法是使用dirname()函数和 PHP__FILE__常量。否则,每当您将文件移动到不同目录或具有不同文件结构的服务器时,您都需要使用新的绝对路径编辑文件。

dirname( __FILE__ )

__FILE__is a constant defined by PHP as the full path to the file from which it is called. Even if the file is included, __FILE__will ALWAYS refer to the full path of the file itself -- not the file doing the including.

__FILE__是一个常量,由 PHP 定义为调用它的文件的完整路径。即使包含文件,__FILE__也将始终引用文件本身的完整路径——而不是执行包含的文件。

So dirname( __FILE__ )returns the full directory path to the directory containing the file -- no matter where it is included from and basename( __FILE__ )returns the file name itself.

所以dirname( __FILE__ )返回包含文件的目录的完整目录路径——无论它从哪里包含并basename( __FILE__ )返回文件名本身。

example: Let's pretend "/home/user/public_html/index.php" includes "/home/user/public_html/your_directory/your_php_file.php".

例如:假设“/home/user/public_html/index.php”包含“/home/user/public_html/your_directory/your_php_file.php”。

If you call dirname( __FILE__ )in "your_php_file.php" you would get "/home/user/public_html/your_directory" returned even though the active script is in "/home/user/public_html" (note the absence of the trailing slash).

如果您调用dirname( __FILE__ )“your_php_file.php”,即使活动脚本在“/home/user/public_html”中,您也会返回“/home/user/public_html/your_directory”(注意没有尾部斜杠)。

If you need the directory of the INCLUDING file use: dirname( $_SERVER['PHP_SELF'] )which will return "/home/user/public_html" and is the same as calling dirname( __FILE__ )in the "index.php" file since the relative paths are the same.

如果您需要包含文件的目录,请使用:dirname( $_SERVER['PHP_SELF'] )这将返回“/home/user/public_html”并且与调用dirname( __FILE__ )“index.php”文件相同,因为相对路径是相同的。

example usages:

示例用法:

@include dirname( __FILE__ ) . '/your_include_directory/your_include_file.php';

@require dirname( __FILE__ ) . '/../your_include_directory/your_include_file.php';

回答by billynoah

In addition to the accepted answer above, you can also use:

除了上面接受的答案外,您还可以使用:

chdir(__DIR__);

回答by John Parker

Another possibility is that the CLI version is using a different php.ini file. (By default, it'll use php-cli.ini and fallback to the standard php.ini)

另一种可能性是 CLI 版本使用的是不同的 php.ini 文件。(默认情况下,它将使用 php-cli.ini 并回退到标准的 php.ini)

Also, if you're using .htaccess files to set your library path, etc. this obviously won't work via the cli.

此外,如果您使用 .htaccess 文件来设置库路径等,这显然无法通过 cli 工作。

回答by Jan Han?i?

When executed trough a cron job your PHP script probably runs in different context than if you start it manually from the shell. So your relative paths are not pointing to the right path.

当通过 cron 作业执行时,您的 PHP 脚本可能会在与您从 shell 手动启动不同的上下文中运行。所以你的相对路径没有指向正确的路径。

回答by Jordan Rumpelstiltskin Nemrow

The DIR works although it will not work on my localhost as it has a different path than my live site server. I used this to fix it.

尽管 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';
    }