Include a file from the root - php

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 ?

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.

Warning: include(dbconn.php): failed to open stream: No such file or directory

I have a site that I am have several issues with. I am not sure if the hosting company did changed something or the site was hacked. I hope this is OK as I have listed a list of errors when a user tries to register for the site. This is also happening when a user tries to use the search function, however in the search function the errors appear at the top and then the search results actually display below. The registration page does not show anything but the following errors.
Warning: include(dbconn.php): failed to open stream: No such file or directory in /home1/hoapres/public_html/users/register/index.php on line 26
Warning: include(dbconn.php): failed to open stream: No such file or directory in /home1/hoapres/public_html/users/register/index.php on line 26
Warning: include(): Failed opening 'dbconn.php' for inclusion (include_path='.:/opt/php54/lib/php') in /home1/hoapres/public_html/users/register/index.php on line 26
Warning: include(functions.php): failed to open stream: No such file or directory in /home1/hoapres/public_html/users/register/index.php on line 27
Warning: include(functions.php): failed to open stream: No such file or directory in /home1/hoapres/public_html/users/register/index.php on line 27
Warning: include(): Failed opening 'functions.php' for inclusion (include_path='.:/opt/php54/lib/php') in /home1/hoapres/public_html/users/register/index.php on line 27
Fatal error: Call to undefined function userText2Web() in /home1/hoapres/public_html/users/register/index.php on line 40
Without knowing your directory structure it is tricky to offer a precise answer, but the first thing I would do before trying to include files is to explicitly set your include path.
Assuming that the site root ( DOCUMENT_ROOT ) is /home1/hoapres/public_html and you have a folder for storing all your include files called includes, the path would be /home1/hoapres/public_html/includes
<?php
set_include_path( $_SERVER['DOCUMENT_ROOT'] . '/includes' );
if( !realpath( get_include_path() ) ) exit('Bad path');
/* Test to see what files exist perhaps */
print_r( glob( get_include_path() . '/*.*' ) );
/* Can you see the files you expect? */
include 'dbconn.php';
include 'functions.php';
?>
Your problem is include path you have used is not correct.
Please check your code where you have include the files like
include("dbconn.php");
you can try by this way
$fullpath = "http://yourdomain.com/";
if the dbconn.php is under any folder then
$fullpath = "http://yourdomain.com/foldername/";
include($fullpath."dbconn.php");

php set_include_path drives me crazy

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.

Php Syntax error question

Hi i have the following directory structure:
Main Folder -> Classes -> user_classes
all nested in each other. I have the Following files inside Classes directory
always_include_top.php
custom_error_handler.php
config.php
database.php
Out of which the database.php file is as follows:
<?php
include_once("always_include_top.php");
include_once("config.php");
include_once("custom_error_handler.php");
include_once ("user_classes/newDatabase.php");
class Database extends newDatabase
{
// some more code... with extra functions
public function dbBackUp($backupfile = NULL)
{
//code...
}
}
?>
I have the Following file at User user_classes directory
newDatabase.php
The code sample for this file is
<?php
include_once("../always_include_top.php");
include_once("../config.php");
include_once("../custom_error_handler.php");
error_reporting(E_ALL);
class newDatabase
{
// my code goes here
}
?>
Why do i get the following error in classes/database.php ( No error in classes/user_classes/newDatabase.php )
Warning: include_once(../always_include_top.php)
[function.include-once]: failed to open stream: No such file or
directory in
E:\wamp\www\greeting_cards\adm\classes\user_classes\database.php on
line 2
Warning: include_once() [function.include]: Failed opening
'../always_include_top.php' for inclusion
(include_path='.;C:\php\pear') in
E:\wamp\www\greeting_cards\adm\classes\user_classes\database.php on
line 2
Warning: include_once(../config.php) [function.include-once]: failed
to open stream: No such file or directory in
E:\wamp\www\greeting_cards\adm\classes\user_classes\database.php on
line 4
Warning: include_once() [function.include]: Failed opening
'../config.php' for inclusion (include_path='.;C:\php\pear') in
E:\wamp\www\greeting_cards\adm\classes\user_classes\database.php on
line 4
Warning: include_once(../custom_error_handler.php)
[function.include-once]: failed to open stream: No such file or
directory in
E:\wamp\www\greeting_cards\adm\classes\user_classes\database.php on
line 5
Warning: include_once() [function.include]: Failed opening
'../custom_error_handler.php' for inclusion
(include_path='.;C:\php\pear') in
E:\wamp\www\greeting_cards\adm\classes\user_classes\database.php on
line 5
Fatal error: Cannot redeclare class Database in
E:\wamp\www\greeting_cards\adm\classes\database.php on line 12
I want both files to individually compile. As i would be including the files in other files based on page types. Whats the problem with including here?
When you use include_once(), it doesn't change to the directory where a particular file is located and execute it there, it executes the contents of that file within the context of the current file. So, classes/user_classes/database.php is executed as if it were in the classes folder. ".." refers to the Main folder in that case, so it's looking for the first three files in the Main folder. Those files aren't in the Main folder, so it gives you the warnings.

How can I set an absolute path for include function in php above the working directory?

I am running a script from
/wp-content/themes/currenttheme/chat.php
I want to include in the above php another one located in
/forum/chat/index.php
The index.php includes its own files
I already tried
$root = $_SERVER['DOCUMENT_ROOT'];
include($root."/forum/chat/index.php");
but I get this error
Warning: require(D:/My Dropbox/xampp/htdocs/lib/custom.php) [function.require]: failed to open stream: No such file or directory in D:\My Dropbox\xampp\htdocs\forum\chat\index.php on line 17
Fatal error: require() [function.require]: Failed opening required 'D:/My Dropbox/xampp/htdocs/lib/custom.php' (include_path='.;\My Dropbox\xampp\php\PEAR') in D:\My Dropbox\xampp\htdocs\forum\chat\index.php on line 17
(the index.php also includes some files, but the /forum/chat is ommited somehow in the path)
then I tried
$path = getcwd();
$myfile = "/forum/chat/index.php";
include ($path.$myfile);
and got this error:
Warning: include(D:\My Dropbox\xampp\htdocs\forum/forum/chat/index.php) [function.include]: failed to open stream: No such file or directory in D:\My Dropbox\xampp\htdocs\wp-content\themes\currenttheme\chat.php on line 24
Warning: include() [function.include]: Failed opening 'D:\My Dropbox\xampp\htdocs\forum/forum/chat/index.php' for inclusion (include_path='.;\My Dropbox\xampp\php\PEAR') in D:\My Dropbox\xampp\htdocs\wp-content\themes\currenttheme\chat.php on line 24
There is no problem with index.php. It is being included.
The error message says about custom.php file
Just use the same $_SERVER['DOCUMENT_ROOT'] technique for the custom.php
you have to add /forum/chat manually as there is no path to be omitted
Something wrong with:
include('../../../forum/chat/index.php');
?
There are all sorts of reasons why the code you've published will fail.
C.
use this.
require_once(ABSPATH.'forum/chat/index.php');
here ABSPATH = WordPress physical root directory path with trailing slash

Categories