$_SERVER['DOCUMENT_ROOT'] 在通过 cron 运行的 php 脚本中不起作用

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

$_SERVER['DOCUMENT_ROOT'] does not work in the php script running through cron

phpcron

提问by phpian

I use $_SERVER['DOCUMENT_ROOT']."/lib/sft_required.php"; to include the 'sft_required' file in a PHP script. When I run this file using browser, it works fine but when I run this as a cron job job, it does not work. It seems that the file is not included when we run the script through cron.

我使用 $_SERVER['DOCUMENT_ROOT']."/lib/sft_required.php"; 在 PHP 脚本中包含“sft_required”文件。当我使用浏览器运行此文件时,它工作正常,但是当我将它作为 cron 作业运行时,它不起作用。当我们通过cron运行脚本时,似乎没有包含该文件。

采纳答案by Quentin

Assuming you are running the script directly through cron (as opposed to from a web server accessed by an HTTP request triggered by a cronjob (e.g. by cron running wget)), then of course it doesn't work.

假设您直接通过 cron 运行脚本(而不是从由 cronjob(例如,通过 cron 运行 wget)触发的 HTTP 请求访问的 Web 服务器),那么它当然不起作用。

There is no server, so $_SERVERis not set.

没有服务器,所以$_SERVER没有设置。

回答by Gabriel Solomon

you could populate the $_SERVER['DOCUMENT_ROOT'] on your own

你可以自己填充 $_SERVER['DOCUMENT_ROOT']

$_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__);

if the cron file is in document root

如果 cron 文件在文档根目录中

$_SERVER['DOCUMENT_ROOT'] = dirname(dirname(__FILE__));

if the cron file is one directory above the document root

如果 cron 文件是文档根目录上方的一个目录

回答by Ignacio Vazquez-Abrams

$_SERVERcannot be expected to contain any of the normal values when a PHP script is run using the CLI interpreter. Either put the path in an environment variable, or pass it to the script as a command line argument.

$_SERVER当使用 CLI 解释器运行 PHP 脚本时,不能期望包含任何正常值。要么将路径放在环境变量中,要么将其作为命令行参数传递给脚本。

回答by ImpressTheNet Web Developer

I answered a similar question here. As people have mentioned, the superglobal $_SERVER isn't defined in CLI situations. In the link is a (so far) foolproof method for obtaining the DOCUMENT_ROOT location. Hope it proves useful.

在这里回答了一个类似的问题。正如人们所提到的,超全局 $_SERVER 未在 CLI 情况下定义。在链接中是一种(到目前为止)获取 DOCUMENT_ROOT 位置的万无一失的方法。希望它被证明是有用的。

回答by Destralak

define('DOCROOT', substr(str_replace(pathinfo(__FILE__, PATHINFO_BASENAME), '', __FILE__), 0, -1));

This will get you the same data as $_SERVER['DOCUMENT_ROOT']for cronjobs.

这将为您提供与$_SERVER['DOCUMENT_ROOT']cronjobs相同的数据。

回答by Дмитрий Гуров

Example 1:
/var/www/site.com/ - DOCUMENT_ROOT;
/var/www/site.com/cron/script.php - CRON PHP script;

示例 1:
/var/www/site.com/ - DOCUMENT_ROOT;
/var/www/site.com/ cron/script.php - CRON PHP 脚本;

<?php
/** DOCUMENT_ROOT -> /var/www/site.com/ */
$_SERVER['DOCUMENT_ROOT'] = realpath(dirname(__FILE__).'/../');
?>

Example 2:
/var/www/site.com/ - DOCUMENT_ROOT;
/var/www/site.com/sub_dir/cron/script.php - CRON PHP script;

示例 2:
/var/www/site.com/ - DOCUMENT_ROOT;
/var/www/site.com/ sub_dir/cron/script.php - CRON PHP 脚本;

<?php
/** DOCUMENT_ROOT -> /var/www/site.com/ */
$_SERVER['DOCUMENT_ROOT'] = realpath(dirname(__FILE__).'/../../');
?>

回答by aimar

I had same problem.. And solutions what i found on internet not worked with my webserver cron, so i needed find another way to change that path easly..

我有同样的问题..我在互联网上找到的解决方案不适用于我的网络服务器 cron,所以我需要找到另一种方法来轻松更改该路径..

And its mostly not big problem, when yu have 1-2 cron files(can easly edit file path if needed), but i had 20 cron files and when i need change server or path changed or smt, then i must change all those files, change file path on them...

它主要不是大问题,当你有 1-2 个 cron 文件时(如果需要,可以轻松编辑文件路径),但我有 20 个 cron 文件,当我需要更改服务器或路径或 smt 时,我必须更改所有这些文件,更改它们的文件路径...

So i found atleast FOR ME exellent solutions:i maded one file path.php in cron folderand bc its same folder with cron files, then you can include it without errors.

所以我至少为我找到了优秀的解决方案:在 cron 文件夹中创建了一个文件 path.php,并在同一个文件夹中创建了 cron 文件,然后你可以毫无错误地包含它。

And in path.php i have $path = '/server/root/path';

path.php 我有$path = '/server/root/path';

And then i include that path.php to my cron files(i have 20 cron files or so)

然后我将该 path.php 包含到我的 cron 文件中(我有 20 个左右的 cron 文件)

And now i use that $path on my cron files as below:

现在我在我的 cron 文件上使用 $path 如下:

include 'path.php';
include $path.'/includes/db.php';

Now if i need change path, then i just open path.php file, change it and all working.

现在,如果我需要更改路径,那么我只需打开 path.php 文件,更改它并且一切正常。

Hope i helped someone, bc that solutions changed my life much much easyer! Its still not perfect, bc perfect would be when all worked automatically, but for me that is much much easyer than previous system, so i tought i will share my experience, mabe i can help someone :)!

希望我帮助了某人,因为解决方案让我的生活变得更轻松了!它仍然不完美,当一切自动运行时,bc 将是完美的,但对我来说,这比以前的系统容易得多,所以我想我会分享我的经验,也许我可以帮助别人:)!

回答by VoidZA

Here is my solution for a similar problem that I dealt with.

这是我处理过的类似问题的解决方案。

$site_root_directory = substr( __DIR__, 0, strpos( __DIR__, "wp-content" ) );

$site_root_directory = substr( __DIR__, 0, strpos( __DIR__, "wp-content" ) );

In my case, I am running a cron that executes a file inside of my theme. (I don't load WordPress for the corn, so I do not have access to things like ABSPATH).

就我而言,我正在运行一个 cron,它在我的主题中执行一个文件。(我没有为玉米加载 WordPress,所以我无法访问诸如ABSPATH)。

This way I get my 'root directory' by using the firstchild folder inside of the root leading to my script, instead of working my way back from the script directory.

这样,我通过使用通向我的脚本的根目录中的第一个子文件夹来获取我的“根目录” ,而不是从脚本目录返回。

/var/more_directories/public_html/wp-content/themes/theme-name/includes/cron-jobs/var/more_directories/public_html/

/var/more_directories/public_html/wp-content/themes/theme-name/includes/cron-jobs/var/more_directories/public_html/

This means that I don't have to be worried about moving my script around, since it will always be somewhere under that first child folder.

这意味着我不必担心移动我的脚本,因为它总是在第一个子文件夹下的某个地方。

回答by Pikamander2

In my case, this issue was caused by the $_SERVER['DOCUMENT_ROOT']path having a trailing slash during normal site operations and no trailing slash during cron jobs.

就我而言,此问题是由于$_SERVER['DOCUMENT_ROOT']在正常站点操作期间路径具有尾部斜杠而在 cron 作业期间没有尾部斜杠引起的。

I'm not sure what was causing that to happen, but as a workaround, I switched my code to use ABSPATHinstead, since it appeared to be returning a consistent value.

我不确定是什么导致了这种情况,但作为一种解决方法,我将代码改为使用ABSPATH,因为它似乎返回了一致的值。

So, in other words, I changed this:

所以,换句话说,我改变了这个:

$private_folder = realpath($_SERVER['DOCUMENT_ROOT'] . "../private_html");

to this:

对此:

$private_folder = realpath(ABSPATH . "../private_html");

There are several other solutions to that problem as well, such as using str_replaceor rtrim.

该问题还有其他几种解决方案,例如使用str_replacertrim