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

后端 未结 9 1710
借酒劲吻你
借酒劲吻你 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:05

    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.

提交回复
热议问题