PHP $_SERVER['DOCUMENT_ROOT'] is empty when called from a Cron Tab

后端 未结 2 421
我寻月下人不归
我寻月下人不归 2021-01-22 14:26

When I use the following code:


I get a nice list off all server variables. However, when I enter the same code i

相关标签:
2条回答
  • 2021-01-22 14:54

    When you execute via cron, are you just calling php myscript.php or are you accessing the php script via http? If you're executing directly via http then the apache variables (such as DocumentRoot) will not be available.

    0 讨论(0)
  • 2021-01-22 15:00

    It's best not to rely on this variable as it isn't always set (just as you discovered).

    Try setting and using a constant like this instead:

    define('PUBLIC_PATH', '/var/www/path/to/public');
    
    // OR something like:
    
    define('PUBLIC_PATH', dirname(__DIR__) . '/public');
    

    Now use PUBLIC_PATH instead of $_SERVER['DOCUMENT_ROOT']

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