【问题标题】:PHP include_once works in local but call in cron not workingPHP include_once 在本地工作,但在 cron 中调用不起作用
【发布时间】:2017-09-09 04:28:50
【问题描述】:

我使用 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");
?>

错误:

[root@xx-xxx-xx~]#php /var/www/html/cloud/cloud_ip_automation/cron_all.php

PHP 警告:include_once(/cloud/references/library.php):未能 打开流:中没有这样的文件或目录 /var/www/html/cloud/cloud_ip_automation/cron_all.php 在第 3 行

警告:include_once(/cloud/references/library.php):打开失败 流:中没有这样的文件或目录 /var/www/html/cloud/cloud_ip_automation/cron_all.php 在第 3 行

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

警告:include_once():打开失败 '/cloud/references/library.php' 用于包含 (include_path='.:/usr/share/pear:/usr/share/php') 在 /var/www/html/cloud/cloud_ip_automation/cron_all.php 在第 3 行

【问题讨论】:

标签: php cron


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    这将创建一个绝对路径,但相对于文件

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

    【讨论】:

      猜你喜欢
      • 2011-12-23
      • 2018-12-02
      • 2016-02-24
      • 1970-01-01
      • 2016-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多