CLI fails to include files

Support/Development for PHP
tvanerp
Forum User
Forum User
Posts: 21
Joined: Thu Apr 24, 2008 4:58 am

CLI fails to include files

Unread post by tvanerp »

I have a strange problem.

For some reason unknown to me the php cli fails to include other php files via 'include', 'require' and all realted functions. When I call the same script from a webrowser it works fine. An example:

file1.php:

Code: Select all

<? print("test"); ?>
file2.php:

Code: Select all

<? require("file1.php"); ?>
File1.php runs fine, but when I try to run file2.php it fails with a fatal error: PHP Fatal error: require(): Failed opening required 'file1.php'

I tried all variations of paths to the file. Full path, relative path etc. I tried changing the include path, all without any result. All variations run fine when called from the web browser. It all ran fine before, I think it stopped working when I upgraded php, but I'm not sure.

This is my php version:

Code: Select all

PHP 5.2.11 (cli) (built: Oct  7 2009 08:32:32)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
    with eAccelerator v0.9.5.3, Copyright (c) 2004-2006 eAccelerator, by eAccelerator
    with the ionCube PHP Loader v3.3.1, Copyright (c) 2002-2009, by ionCube Ltd.
    with Suhosin v0.9.27, Copyright (c) 2007, by SektionEins GmbH
Any help would be greatly appreciated, since i now have a hard time running cron jobs.

Kind regards,
tijs
Highland
Forum Regular
Forum Regular
Posts: 674
Joined: Mon Apr 10, 2006 12:55 pm

Re: CLI fails to include files

Unread post by Highland »

When calling any include file for a CLI call you have to put the full file path. A good trick to get around this is to use dirname()

Code: Select all

$fpath=dirname(__FILE__);
require_once $fpath.'/file.php';
But now that you mention it, I do have a cron job that mysteriously stopped running last week. Runs fine if I call it from the CLI manually. I haven't touched PHP lately, tho. 5.2.11-2 is fairly old so I doubt it would be that.
"Its not a mac. I run linux... I'm actually cool." - scott
tvanerp
Forum User
Forum User
Posts: 21
Joined: Thu Apr 24, 2008 4:58 am

Re: CLI fails to include files

Unread post by tvanerp »

Highland wrote:When calling any include file for a CLI call you have to put the full file path. A good trick to get around this is to use dirname()

Code: Select all

$fpath=dirname(__FILE__);
require_once $fpath.'/file.php';
But now that you mention it, I do have a cron job that mysteriously stopped running last week. Runs fine if I call it from the CLI manually. I haven't touched PHP lately, tho. 5.2.11-2 is fairly old so I doubt it would be that.
I tried it that way as well, I even set the full path manually. It all didn't work.
Post Reply