Relative path not working in cron PHP script

前端 未结 8 1501
甜味超标
甜味超标 2020-11-28 07:59

If a PHP script is run as a cron script, the includes often fail if relative paths are used. For example, if you have

require_once(\'foo.php\');
相关标签:
8条回答
  • 2020-11-28 08:42

    The DIR works although it will not work on my localhost as it has a different path than my live site server. I used this to fix it.

        if(__DIR__ != '/home/absolute/path/to/current/directory'){ // path for your live server
            require_once '/relative/path/to/file';
        }else{
            require_once '/absolute/path/to/file';
        }
    
    0 讨论(0)
  • 2020-11-28 08:44

    Another possibility is that the CLI version is using a different php.ini file. (By default, it'll use php-cli.ini and fallback to the standard php.ini)

    Also, if you're using .htaccess files to set your library path, etc. this obviously won't work via the cli.

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