$_SERVER['DOCUMENT_ROOT'] does not work in the php script running through cron
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.
you could populate the $_SERVER['DOCUMENT_ROOT'] on your own
$_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__);
if the cron file is in document root
$_SERVER['DOCUMENT_ROOT'] = dirname(dirname(__FILE__));
if the cron file is one directory above the document root
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.
There is no server, so $_SERVER
is not set.
$_SERVER
cannot 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.
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.
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.
Example 1:
/var/www/site.com/ - DOCUMENT_ROOT;
/var/www/site.com/cron/script.php - CRON PHP script;
<?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;
<?php
/** DOCUMENT_ROOT -> /var/www/site.com/ */
$_SERVER['DOCUMENT_ROOT'] = realpath(dirname(__FILE__).'/../../');
?>
Here is my solution for a similar problem that I dealt with.
$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
).
This way I get my 'root directory' by using the first child 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/
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.
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..
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...
So i found atleast FOR ME exellent solutions: i maded one file path.php in cron folder and bc its same folder with cron files, then you can include it without errors.
And in path.php i have $path = '/server/root/path';
And then i include that path.php to my cron files(i have 20 cron files or so)
And now i use that $path on my cron files as below:
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.
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 :)!
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.
I'm not sure what was causing that to happen, but as a workaround, I switched my code to use ABSPATH
instead, since it appeared to be returning a consistent value.
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_replace
or rtrim
.
This solution worked for me:
$site_root_directory = substr( __DIR__, 0, strpos( __DIR__, "public_html" ) ).'public_html';
精彩评论