簡體   English   中英

PHP:chdir('../')無法從命令行運行

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

我正在開發一個PHP cron腳本,以檢查服務器是否已啟動(Ping)。

這是我的代碼:

// 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();

因此,在第一行中,我需要創建一個chdir('../'); 包括必要的文件( header.php )。

我使用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

我搜索了為什么PHP刪除了此內容,但找不到任何答案。

有人知道為什么會這樣嗎?

謝謝 !!

當前工作目錄將是您從中執行腳本的位置,而不是腳本所在的位置。 您可以使用常量__DIR__獲得包含腳本的目錄。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM