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 whe
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__).'/../../');
?>
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.
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.