代码之家  ›  专栏  ›  技术社区  ›  eneskomur

require\u once在命令行中不起作用

  •  1
  • eneskomur  · 技术社区  · 9 年前
    php /home/test9/public_html/degerlendir/test-4567.php "var1=18&var2=22"
    

    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
    

    问题是页面不包括 配置。php 在父目录中。页面在浏览器中正常工作。我试着使用不同的require\u,比如 . 我无法让它工作。

    3 回复  |  直到 9 年前
        1
  •  4
  •   Ivo P    9 年前

    从命令行中没有 $_SERVER['DOCUMENT_ROOT'] . 这个只能从http服务器(Apache)上获得。

    __DIR__ 在脚本的开头

    <?php chdir(__DIR__); ?>

        2
  •  3
  •   Amjo    9 年前

    /主页/测试//您的文件

        3
  •  2
  •   Rob Ruchte    9 年前

    在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