3

I use include_once to include file in php it will works in localhost but same file run using cron it shows error

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 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

2
  • You're searching in the wrong directory. Maybe $_SERVER['document_root'] isn't pointing to where you think it's pointing. Commented Apr 13, 2017 at 7:11
  • 1
    Possible duplicate of Relative path not working in cron PHP script Commented Apr 13, 2017 at 7:16

2 Answers 2

6

The $_SERVER variable is not set when running from CLI. You have to use dirname(__FILE__) and make the paths relative to the current file.

For example in your case, something like:

include_once(dirname(__FILE__).'/../'.project_name.'/references/library.php');
Sign up to request clarification or add additional context in comments.

Comments

2

This will create an absolute path, but relative to the file

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.