php set_include_path drives me crazy - php

I need to include a file in php so I do
$web_proc = '../res/proc'; //variable read from config file I can't change
//in my file
include_once($web_proc . '/check_sprache.php');
and PHP outputs:
Warning: include_once(../res/proc/check_sprache.php): failed to open
stream: No such file or directory in
/Users/user/Sites/site/res/pdf/rechnung.php on line 62
Warning:
include_once(): Failed opening '../res/proc/check_sprache.php' for
inclusion (include_path='.:') in
/Users/user/Sites/site/res/pdf/rechnung.php on line 62
So I change the include path:
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__DIR__) . DIRECTORY_SEPARATOR);
and try it again, but PHP outputs:
Warning: include_once(../res/proc/check_sprache.php): failed to open
stream: No such file or directory in
/Users/user/Sites/site/res/pdf/rechnung.php on line 62
Warning:
include_once(): Failed opening '../res/proc/check_sprache.php' for
inclusion (include_path='.::/Users/user/Sites/site/res/') in
/Users/user/Sites/site/res/pdf/rechnung.php on line 62
But if if I do
include_once(dirname(__DIR__). DIRECTORY_SEPARATOR . $pfadweb_proc . '/check_sprache.php');
it works. But this is no solution, since the included file includes more files with a relative path, so they are alse not found.
So either I misunderstand the PHP include path, or it's just trolling me.
Can anybody help?

The include path looks odd in the second example; You have two PATH_SEPARATOR characters and a trailing DIRECTORY_SEPARATOR though I doubt this is the problem.
Try this one out instead
set_include_path(implode(PATH_SEPARATOR, [ // use array() if PHP < 5.4
dirname(__DIR__), // this should be /Users/user/Sites/site/res
get_include_path()
]));
include_once 'proc/check_sprache.php';
This will add the parent directory (/Users/user/Sites/site/res) of the CWD (/Users/user/Sites/site/res/pdf) to your include path.
Any include statements in any other files can use a relative path from /Users/user/Sites/site/res.

Related

iam getting errors as follow Warning: include_once(C:/xampp/htdocs/timetable/header.php): failed to open stream: No such file or directory

Warning: include_once(C:/xampp/htdocs/timetable/header.php): failed to open stream: No such file or directory in C:\xampp\htdocs\timetablegeneratormaster\timetable\index.php on line 4
Warning: include_once(): Failed opening 'C:/xampp/htdocs/timetable/header.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\timetablegeneratormaster\timetable\index.php on line 4
Warning: include_once(C:/xampp/htdocs/timetable/footer.php): failed to open stream: No such file or directory in C:\xampp\htdocs\timetablegeneratormaster\timetable\index.php on line 80
Warning: include_once(): Failed opening 'C:/xampp/htdocs/timetable/footer.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\timetablegeneratormaster\timetable\index.php on line 80
When including files that are in the same directory or to several levels higher or lower than the current file, use the __DIR__ constant to get the path to these files.
If header.php and footer.php are in the same directory as index.php, use this code:
include_once(__DIR__ . "/header.php");
include_once(__DIR__ . "/footer.php");
If they are in a subdirectory, e.g. includes then use the code:
include_once(__DIR__ . "/includes/header.php");
include_once(__DIR__ . "/includes/footer.php");
However, if they were in another place, you use such code:
include_once(__DIR__ . "/../../includes/another.php");
The __DIR__ contains the name of the current directory where the php file is located in which the constant is used.

Include a file from the root

Here is the structure of my application :
core
application
controller_base.class.php
registry.class.php
router.class.php
template.class.php
controller
include
config.php
model
views
index.php
The file config.php is included in the index.php file (there's no problem with this) and here is what it contains :
<?php
/*** include the controller class ***/
include(ROOT . '/core/application/controller_base.class.php');
/*** include the registry class ***/
include(ROOT . '/core/application/registry.class.php');
/*** include the router class ***/
include(ROOT . '/core/application/router.class.php');
/*** include the template class ***/
include(ROOT . '/core/application/template.class.php');
/*** autoload model classes ***/
function __autoload($class_name) {
$filename = strtolower($class_name) . '.class.php';
$file = ROOT . '/core/model/' . $filename;
if (!file_exists($file)) {
return false;
}
// include the $class_name class
include($file);
}
$registry = new registry;
You have to know that the constant ROOT is defined like this :
define('ROOT', str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']));
This constant contains the value /Lab/Livre/POO/TP1 which is of course the root of the applications.
But there's a problem whith all the files I include in config.php. PHP throws me an error that says the files are not found in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php.
So the problem is clear. I want PHP to look for the files from the root of the application (where index.php is), but it looks for it from the folder where config.php is located.
I had this problem for a long time but I want to find a solution once and for all.
I can't get it to do want I want ! I hope someone will help me.
Thank you very much.
P.S.: In other words, i want to include the files using an absolute path.
P.S.: Here is the full error text :
Warning: include(/Lab/Livre/POO/TP1/core/application/controller_base.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 4
Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/controller_base.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 4
Warning: include(/Lab/Livre/POO/TP1/core/application/registry.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 7
Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/registry.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 7
Warning: include(/Lab/Livre/POO/TP1/core/application/router.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 10
Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/router.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 10
Warning: include(/Lab/Livre/POO/TP1/core/application/template.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 13
Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/template.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 13
I have found a solution to my problem. I set the ROOT constant to this :
define('ROOT', $_SERVER['DOCUMENT_ROOT'] . str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']));
But what went wrong ?

phpsec lib path is not working from cron job

The file path for my script is
/var/www/html/MyProject/index.php
when I run the script as
~/./Myproject$ php index.php its runs perfectly
When I run the script as
~$ php /var/www/html/MyProject/index.php
It does not read the phpseclib file path
My index.php file is
<?php
include("crud.php");
include("functions.php");
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include('Net/SFTP.php');
...
?>
error:
PHP Warning: include(Net/SFTP.php): failed to open stream: No such file or directory in /var/www/html/MyProject/index.php on line 6
PHP Warning: include(): Failed opening 'Net/SFTP.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear.:/usr/share/php:/usr/share/pear/phpseclib') in /var/www/html/MyProject/index.php on line 6
PHP Fatal error: Class 'Net_SFTP' not found in /var/www/html/MyProject/index.php on line 186
How to run the php script form cron job?
From the error message, you can see that phpseclib is not being added to the include path correctly. Try this for the set_include_path instead:
set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . DIRECTORY_SEPARATOR . 'phpspeclib');
The __DIR__ will take into account the location of the file you're executing, rather than trying to find phpspeclib relative to the current working directory.
You can try placing phpseclib1.0.12 library to the /usr/share/ or if you are running from the Project then you can try adding like this:
set_include_path('/var/www/html/phpseclib1.0.12');
include('Net/SFTP.php');

Use of undefined constant in PHP while generationg directory path

i am using this code to define the directory paths of my project:
config.php is
<?php
define('CB_HOME', realpath(dirname(__DIR__)));
define('FD_HOME', CB_HOME."/testbot");
I use am using CB_home And FD_home in other files.
i am getting the error:
PHP Notice: Use of undefined constant CB_HOME - assumed 'CB_HOME' in C:\\xampp\\htdocs\\test-bot\\testbot\\bootstrap.php on line 3
PHP Warning: require_once(CB_HOME/AbstractCbRest.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory in C:\\xampp\\htdocs\\test-bot\\testbot\\bootstrap.php on line 3
PHP Fatal error: require_once() [<a href='function.require'>function.require</a>]: Failed opening required 'CB_HOME/AbstractCbRest.php' (include_path='.;C:\\xampp\\php\\PEAR') in C:\\xampp\\htdocs\\test-bot\\testbot\\bootstrap.php on line 3
my bootstrap.php is :
<?php
require_once 'config.php';
require_once CB_HOME.'/AbstractCbRest.php';
where is it going wrong?
My guess is that include_path settings are interfering with your script. So (as mentioned by #deceze) you might be loading an incorrect (although existing) config.php file.
Maybe you could try using an absolute path to include your configuration or use something along the lines of:
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config.php';
It is obvious that the windows path with two "\" doesn't work. To remove a double backslash (NOTE: this is no solution but a workaround), do this:
$someValue = 'C:\\xampp\\htdocs\\test-bot\\testbot\\'; $someValue = preg_replace('/\\\\/u', '\\', $someValue);
or in your case:
define('CB_HOME', preg_replace('/\\\\/u', '\\', realpath(dirname(DIR))));

Zend Loader Problem

Well this question maybe asked for hunderds time. But I could not make it work.
There is my directory list:
--main/
---- Zend/
---- dir1/
---- dir2/
And this is my set include path configuration:
set_include_path(PATH_SEPARATOR
. dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Zend' . PATH_SEPARATOR
. get_include_path());
I also try adding all directories seperatly to the path.
But apache insists on giving these error:
Warning: require_once(Zend/Loader.php): failed to open stream: No such file or directory in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\main\Zend\Translate.php on line 25 Fatal error: require_once(): Failed opening required 'Zend/Loader.php' (include_path=';C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\main\Zend;.;C:\php\pear') in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\main\Zend\Translate.php on line 25
Which is thrown from the php file which is in dir1 on these lines:
include_once '../Zend/Locale.php';
include_once '../Zend/Translate.php'; //this is the line
How can I solve this problem?
As you have a relative to current file include path, one of the two Zend classes, which is running:
require_once 'Zend/Loader.php';
is resolving to htdocs\main\Zend\Zend\Loader.php
To fix, remove the DIRECTORY_SEPARATOR . 'Zend', then also your include_once's can be
require_once 'Zend/Locale.php';
require_once 'Zend/Translate.php';
Note I changed to require_once as include_once will carry on the script even if the include fails, which is not recommended.

Categories