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.
10 Answers
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.
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.