2017-04-13 69 views
1

叫我應該使用include_once爲包括將在本地主機,但同一個文件運行Works如何使用cron PHP文件時,它會顯示錯誤PHP在當地include_once工作,但在cron不工作

File name : cron_all.php 
<?php 
    define('project_name','/cloud'); 
    include_once($_SERVER['DOCUMENT_ROOT'].project_name."/references/library.php"); 
?> 

錯誤:

[[email protected]~]# php /var/www/html/cloud/cloud_ip_automation/cron_all.php

PHP Warning: include_once(/cloud/references/library.php): failed to open stream: No such file or directory in /var/www/html/cloud/cloud_ip_automation/cron_all.php on line 3

Warning: include_once(/cloud/references/library.php): failed to open stream: No such file or directory in /var/www/html/cloud/cloud_ip_automation/cron_all.php on line 3

PHP Warning: include_once(): Failed opening '/cloud/references/library.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/html/cloud/cloud_ip_automation/cron_all.php on line 3

Warning: include_once(): Failed opening '/cloud/references/library.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/html/cloud/cloud_ip_automation/cron_all.php on line 3

+0

你在錯誤的目錄搜索。也許'$ _SERVER ['document_root']'不是指向你認爲它指向的地方。 – Albzi

+1

[相對路徑不能在cron PHP腳本中工作]的可能重複(http://stackoverflow.com/questions/1969374/relative-path-not-working-in-cron-php-script) –

回答

2

從CLI運行時未設置$ _SERVER變量。 您必須使用dirname(__FILE__)並且創建相對於當前文件的路徑。

例如,在你的情況,是這樣的:

include_once(dirname(__FILE__).'/../'.project_name.'/references/library.php'); 
+0

感謝它的工作.. – sridhard

1

這將創建一個絕對路徑,但相對於文件

include_once dirname(__FILE__) . '/../'.project_name.'/references/library.php'; 
+0

感謝它的工作.. – sridhard