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
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.
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
Related
I try to run my php script from python.
Here is my php code:
<?php
require '../functions/server_info.php';
require ('../functions/functions.php');
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
$input_data_trunk = array();
$input_data_trunk['ami_category']="TRK-IPU";
$IP = "192.168.1.200";
conf_sip_action($input_data_trunk, 'new', 'trunk');
echo "done";
?>
Then I write python code i execute this script, but I have error like this:`
PHP Warning: require(../functions/server_info.php): failed to open stream: No such file or directory in /var/www/html/IPU-GUI/website2/LTU_trunk.php on line 2
PHP Fatal error: require(): Failed opening required '../functions/server_info.php' (include_path='.:/usr/share/php') in /var/www/html/IPU-GUI/website2/LTU_trunk.php on line 2
Here is my python code (just execute script):
import subprocess
subprocess.call( ["/usr/bin/php", "/var/www/html/IPU-GUI/website2/LTU_trunk.php"] )
I am looking for help.
Thanks in advance.
`
require .. is relative to the current working directory/the PATH setting, not the current PHP file. When executing from Python, the working directory will be either undefined or simply different from what you might expect. Use an absolute import path/relative to the file:
require dirname(__DIR__) . '/functions/server_info.php';
I have PHPUnit set up in my PHPStorm project. I've referenced the PHPUnit phar file and have a PHP executable linked to my PHPStorm run configuration. My directory structure looks like:
/lib/classes/Class.php
/lib/vendor/phpunit.phar
/lib/test/ClassTest.php
In my ClassTest.php file, I reference the other two files with:
require_once (__DIR__ . "../vendor/phpunit.phar");
require_once (__DIR__ . "../classes/Class.php");
I get the following error when I run my test:
Fatal error: require_once(): Failed opening required
'C:\Users\me\PhpstormProjects\myproject\lib\tests../vendor/phpunit.phar'
It seems like the PHP parser isn't correctly parsing the up one directory ../ command.
Why is this happening?
First of all: this line is not needed (at all) as PHPUnit will already be loaded at that time.
require_once (__DIR__ . "../vendor/phpunit.phar");
Secondly: __DIR__ constant in PHP does not contain trailing slash. When used in require/include statements (and other places when building full file path) you have to add it yourself.
In your particular case it has to be (note / before ..):
require_once (__DIR__ . "/../classes/Class.php");
I am working on ubuntu 12.04. I installed HTTP_Client by sudo pear install HTTP_Client. But when I am using require_once 'HTTP/Client.php';. It's showing:
Warning: require_once(HTTP/Client.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory in /var/www/mai.php on line 3
How I remove this error?
How paths are resolved when using include or require is controlled by the include_path php.ini setting, which is typically set to:
include_path=".:/usr/share/php"
Whereby /usr/share/php points to where PEAR is installed.
It can be set using set_include_path() during runtime as well:
set_include_path(".:/usr/share/php");
Should be run before you include anything else.
'HTTP/Client.php' is a relative path. The error message means that the file doesn't exist at that location. You'll need to modify the path, or move the file.
In this case your PHP file is in "/var/www/" and there is no HTTP directory there.
Edit: I would recommend modifying the path to be the full path rather than the relative path:
require_once 'HTTP/Client.php';
Change to:
require_once '/usr/share/php/HTTP/Client.php';
These are code of my PHP applications myApp.phpand mypartsA/main.php.
myApp.php
<?php
echo ("##### myApp is starting now !!! </br>");
require_once('/parts/mypartsA/main.php');
?>
mypartsA/main.php
<?php
echo ("#### mypartsA is required </br>");
?>
And myApp.php and mypartsA/main.php are in following structure.
Document_root
|
+--- test
| |
| +--- myApp.php
|
+--- parts
|
+--- mypartsA
|
+---main.php
I’ve used this application on Windows+Apache for a year with no problem but recently I migrate this to CentOS+Apache then I get fatal error related to include_path
PHP Warning: require_once(/parts/mypartsA/main.php)
[<a href='function.require-once'>function.require-once</a>]
: failed to open stream
: No such file or directory
in /var/www/html/test/myApp.php on line 12
PHP Fatal error: require_once() [<a href='function.require'>function.require</a>]
: Failed opening required (/parts/mypartsA/main.php’
(include_path='.:/php/includes:/usr/share/pear:/usr/share/pear/PEAR:/var/www/html')
in /var/www/html/test/myAPP.php on line 12
According to PHP.net if a path is defined the include_path will be ignored.
http://www.php.net/manual/en/function.include.php
I think in my case PHP tries to find parts directory from C:\ or / not from include_path.
require_once(/parts/mypartsA/main.php);
Therefore result of myAPP.php must be fatal error whether on Windows or on CentOS.
But my application has been working for a year with no error on Windows. PHP can find parts directory correctly.
Can sameone try those codes above on Windows and see..
##### myApp is starting now !!!
#### mypartsA is required
or get error?
Now,I want to know is why results of same code are different between running on Windows and running on CentOS.
What dirctive in PHP.ini do I have to check ?
I know if I change this code to add absolute path with $ _SERVER['DOCUMENT_ROOT'].
require_once($_SERVER['DOCUMENT_ROOT']./parts/mypartsA/main.php);
It’ll work well. But there are many applications with same style of coding and I don’t want to change all of them.
These are additional information.
include_path values phpinfo() shows on Windows
.;C:\PHP;C:\PHP\pear;C:\Apache2.2.22\htdocs
include_path values phpinfo() shows on CentOS
.:/php/includes:/usr/share/pear:/usr/share/pear/PEAR:/var/www/html
$path = realpath(dirname(__FILE__)) . "/../../parts/mypartsA/main.php";
require_once($path);
try with this path
$path = "../parts/mypartsA/main.php";
the second path should work with document root.
I am trying to get crontab to run a php file, here is the cronjob
10 * * * * /usr/bin/php /var/www/update/ranks.php >> /var/www/update/log/ranks.txt
But I keep getting an error saying the required file does not exist
PHP Warning: require_once(../mws_products.php): failed to open stream: No such file or directory in /var/www/update/ranks.php on line 2
PHP Fatal error: require_once(): Failed opening required '../mws_products.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/update/ranks.php on line 2
I do not get this problem when I run the file from a browser or when I go into the directory of the file and execute the file e.g. cd /var/www/update/
ranks.php
<?php
require_once('../mws_products.php');
echo "-------------------------------------------------------------\n";
echo date('d-M-Y H:i:s',time())."\n";
echo "Update Ranks\n";
$products->updateRanks();
$database->addUpdate("ranks", time());
echo "\n\n\n";
?>
folder structure
[folder] update
|____ [file] ranks.php
[file] mws_products.php
What could be causing this problem? (note: I have tried restarting apache and the server)
As your are running in a crontab from the root directory ../mws_pruducts.php does not exists, relative to the given root.
There are multiple solutions, this is one of them:
define( 'ROOT', dirname(__FILE__) );
require_once(ROOT . '/../mws_products.php');
Try with the absolute path for the file...
require_once('/var/www/mws_products.php');