简体   繁体   English

PHP:chdir('../')无法从命令行运行

[英]PHP : chdir('../') not working from command line

I'm developing a PHP cron script to check if a server is up or not (Ping). 我正在开发一个PHP cron脚本,以检查服务器是否已启动(Ping)。

Here is my code : 这是我的代码:

// Remonte d'un dossier
chdir('../');

// Inclusion du header pour avoir les infos de connexion à la db, fonctions, etc ...
require_once('./includes/header.php');

// Requête pour récupérer toutes les IP à pinger (Non-exclues donc)
$sReqGetAllServers = "  SELECT
                            *
                        FROM
                            host
                        WHERE
                            exclude_machine = :exclude_machine";

// Préparation de la requête
$oDatabase->Prepare($sReqGetAllServers);

// On bind la valeur au paramètre dans le WHERE
$oDatabase->BindValue(':exclude_machine', 'n', PDO::PARAM_STR);

// Exécution de la requête
$oDatabase->Execute();

// On associe les données dans un tableau à deux dimensions associatif
$aServers = $oDatabase->Assoc();

So at the first line, I need to make a chdir('../'); 因此,在第一行中,我需要创建一个chdir('../'); to include the necessary files ( header.php ). 包括必要的文件( header.php )。

I tested the script with Chrome and all worked fine but when I execute the script with command-line, PHP drop this error : 我使用Chrome浏览器测试了脚本,并且一切正常,但是当我使用命令行执行脚本时,PHP删除了此错误:

[14:40] root@dev.company.local / >> php -f /web/dev/company/public_html/dasPing/cron/cron.php 
PHP Warning:  require_once(./includes/header.php): failed to open stream: No such file or directory in /var/www/html/dev/diadeis/public_html/dasPing/cron/cron.php on line 7

Warning: require_once(./includes/header.php): failed to open stream: No such file or directory in /var/www/html/dev/diadeis/public_html/dasPing/cron/cron.php on line 7
PHP Fatal error:  require_once(): Failed opening required './includes/header.php' (include_path='/usr/share/php') in /var/www/html/dev/diadeis/public_html/dasPing/cron/cron.php on line 7

Fatal error: require_once(): Failed opening required './includes/header.php' (include_path='/usr/share/php') in /var/www/html/dev/diadeis/public_html/dasPing/cron/cron.php on line 7

I searched why PHP drop this but I can't find any answer. 我搜索了为什么PHP删除了此内容,但找不到任何答案。

Do anyone knows why this is happening ? 有人知道为什么会这样吗?

Thank you !! 谢谢 !!

The current working directory would be where you are executing the script from, not where the script lives. 当前工作目录将是您从中执行脚本的位置,而不是脚本所在的位置。 You can get the directory that contains the script using the constant __DIR__ 您可以使用常量__DIR__获得包含脚本的目录。

chdir(__DIR__ . '/../');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM