2017-04-13 74 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