【问题标题】:require_once doesn't work in command linerequire_once 在命令行中不起作用
【发布时间】:2018-01-23 09:10:16
【问题描述】:
php /home/test9/public_html/degerlendir/test-4567.php "var1=18&var2=22"

我需要使用 cron 作业在后台运行一页。我用上面的命令测试了我的代码。但我得到这个错误:

PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/extensions/no-debug-non-zts-20100525/imagick.so' - /usr/lib64/extensions/no-debug-non-zts-20100525/imagick.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning:  require_once(../config.php): failed to open stream: No such file or directory in /home/test9/public_html/degerlendir/test-4567.php on line 2
PHP Fatal error:  require_once(): Failed opening required '../config.php' (include_path='.:/usr/lib64/php') in /home/test9/public_html/degerlendir/test-4567.php on line 2

问题是页面在父目录中不包含 config.php。该页面在浏览器中正常工作。我尝试使用不同的 require_once 变体,例如 require_once ($_SERVER['DOCUMENT_ROOT']."/config.php")。我无法让它工作。

【问题讨论】:

    标签: php centos fatal-error require-once


    【解决方案1】:

    Cron 作业始终从根目录获取包含文件的完整路径。

    /home/test/.../你的文件

    【讨论】:

      【解决方案2】:

      在您的 cron 作业中,您需要 cd 到正确的工作目录以允许您的 PHP 文件找到它的包含。我通过创建小的 shell 脚本来做到这一点,并从 cron 运行 shell 脚本:

      #!/bin/bash
      DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
      cd  ${DIR}
      php test-4567.php
      
      exit 0
      

      这还使您能够执行一些有用的操作,例如检查脚本是否已在运行,以确保您不会启动多个线程(如果您想避免这种情况)。

      #!/bin/bash
      FIND_PROC=`ps -ef |grep "php test-4567.php" | awk '{if ($8 !~ /grep/) print $2}'`
      # if FIND_PROC is empty, the process has died; restart it
      
      if [ -z "${FIND_PROC}" ]; then
              DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
              cd  ${DIR}
              php test-4567.php
      fi
      
      exit 0
      

      【讨论】:

        【解决方案3】:

        在命令行中没有$_SERVER['DOCUMENT_ROOT']。那个只能从 http-server (Apache) 获得。

        不会自动设置工作目录。如果提示当前位于 /some/path/,脚本将尝试在 /some/config.php 中查找 config.php。

        尝试在脚本开头使用__DIR__ cd 到当前路径

        <?php chdir(__DIR__); ?>

        【讨论】:

          猜你喜欢
          • 2021-09-11
          • 1970-01-01
          • 2013-01-24
          • 1970-01-01
          • 2016-01-12
          • 1970-01-01
          • 1970-01-01
          • 2014-04-01
          • 2018-01-04
          相关资源
          最近更新 更多