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

后端 未结 9 1668
借酒劲吻你
借酒劲吻你 2020-12-08 04:35

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

相关标签:
9条回答
  • 2020-12-08 05:25

    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__).'/../../');
    ?>
    
    0 讨论(0)
  • 2020-12-08 05:30
    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.

    0 讨论(0)
  • 2020-12-08 05:32

    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.

    0 讨论(0)
提交回复
热议问题