1

I am trying to include a php file from the parent directory and I getting error:

admin@webby:~$ /usr/bin/php /var/phpscripts/email_parser/tests/_email-test.php PHP Fatal error: require_once(): Failed opening required '../PlancakeEmailParser.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/phpscripts/email_parser/tests/_email-test.php on line 6

Fatal error: require_once(): Failed opening required '../PlancakeEmailParser.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/phpscripts/email_parser/tests/_email-test.php on line 6

PHP file

#!/usr/bin/php
<?php
error_reporting(E_ALL ^ E_NOTICE ^E_WARNING);
ini_set("display_errors" , 1);

require_once("../PlancakeEmailParser.php");

// etc

?>

Folder Structure

admin@webby:/var/phpscripts/email_parser$ find .
.
./composer.json
./README.txt
./LICENSE.txt
./PlancakeEmailParser.php
./tests
./tests/_email-test.php

For testing it works fine when I move PlancakeEmailParser.php into the tests directory and remove the "../" from the require

3
  • Try doing it this way: require_once(__DIR__."/../PlancakeEmailParser.php"); Commented Dec 27, 2012 at 16:59
  • @hakre: how come relative include do not work when calling a php script from command line Commented Dec 27, 2012 at 17:01
  • @JohnMagnolia: They work, but the question is to which base path are they relative too and which one is the current base path PHP uses to resolve them? (Working directory differs) - the other point is PHP's configuration for the include path directive. (php configuration differs) - That are merely the two points I can imagine. Provide the getcwd() and the value of the setting: var_dump(getcwd(), get_include_path()); to explain this better. Commented Dec 27, 2012 at 17:06

2 Answers 2

5

The line

require_once("../PlancakeEmailParser.php");

Is not using a fully qualified file-system path or PHP-URI-scheme. Therefore its outcome depends on PHP configuration, most often because of the include directory configuration.

The PHP CLI can use a different configuration file than with your webserver - or - the working directory is different as with your webserver (probably the later plays more of a role in your specific scenario).

What you want can be easily expressed fully qualified, too:

require_once(__DIR__ . "/../PlancakeEmailParser.php");

This is probably what you're looking for.

Sign up to request clarification or add additional context in comments.

3 Comments

Thats worked thanks. This is my first php script called from command line looks like there are quite a few of these instances where it works different to a standard file inside of the /var/www
Are there any guides/articles which lists the most common issues that I am likely to run into issues like this
Not that I especially know of on top of my head, however, I usually use this __DIR__ special constant. This won't make PHP search a lot and it's clear what it does and it works everywhere.
1

Quote from PHP CLI SAPI: Differences to other SAPIs:

It does not change the working directory to that of the script. (-C and --no-chdir switches kept for compatibility)

In order to keep relative paths in your script working as-is, change directory to where the script resides, then execute the script:

cd /var/phpscripts/email_parser/tests/ && ./_email-test.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.