我使用include_once来包含php中的文件它将在localhost中运行但是使用cron运行相同的文件它会显示错误
File name : cron_all.php
<?php
define('project_name','/cloud');
include_once($_SERVER['DOCUMENT_ROOT'].project_name."/references/library.php");
?>
error:
[root @ xx-xxx-xx~] #php /var/www/html/cloud/cloud_ip_automation/cron_all.php PHP警告:include_once(/cloud/references/library.php):无法打开流:没有这样的文件或第3行/var/www/html/cloud/cloud_ip_automation/cron_all.php中的目录警告:include_once(/cloud/references/library.php):无法打开流:/ var / www /中没有此类文件或目录第3行的html / cloud / cloud_ip_automation / cron_all.php PHP警告:include_once():无法打开'/cloud/references/library.php'以包含(include_path =' . :/ usr / share / pear:/ usr / share第3行/var/www/html/cloud/cloud_ip_automation/cron_all.php中的/ php')警告:include_once():无法打开包含的'/cloud/references/library.php'(include_path =' . :/ usr / share / pear:/ usr / share / php')在第3行的/var/www/html/cloud/cloud_ip_automation/cron_all.php中
2 回答
从CLI运行时,未设置$ _SERVER变量 . 您必须使用
dirname(__FILE__)
并创建相对于当前文件的路径 .例如,在您的情况下,例如:
这将创建一个绝对路径,但相对于文件