PHP include_once 在本地工作但在 cron 中调用不工作

sri*_*ard 2 php cron

我使用 include_once 将文件包含在 php 中,它将在本地主机中工作,但使用 cron 运行相同的文件会显示错误

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

错误:

[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):无法打开流:第 3 行 /var/www/html/cloud/cloud_ip_automation/cron_all.php 中没有此类文件或目录

PHP 警告:include_once():无法在 /var/www/html/ 中打开 '/cloud/references/library.php' 进行包含 (include_path='.:/usr/share/pear:/usr/share/php') cloud/cloud_ip_automation/cron_all.php 第 3 行

警告:include_once():无法在 /var/www/html/cloud 中打开 '/cloud/references/library.php' 进行包含 (include_path='.:/usr/share/pear:/usr/share/php') /cloud_ip_automation/cron_all.php 第 3 行

小智 5

从 CLI 运行时,未设置 $_SERVER 变量。您必须使用dirname(__FILE__)并创建相对于当前文件的路径。

例如,在您的情况下,类似:

include_once(dirname(__FILE__).'/../'.project_name.'/references/library.php');
Run Code Online (Sandbox Code Playgroud)