I have a file that is called header (the header of my site).. I use that file in all my site. The problem is that it has this include in it:
include_once("../untitled/sanitize_string.php");
which means that a mistake may be thrown, depending on who calls the header file.
I have directories, subdirectories and subdirectories to the subdirectories.. is there a simple way to prevent this from happening.. Instead of taking the satize string include and placing it on every page and not in the header file
Warning: require_once(/untitled/sanitize_string.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\headerSite.php on line 7
Fatal error: require_once() [function.require]: Failed opening required '/untitled/sanitize_string.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\headerSite.php on line 7
You may consider setting a global include path while using include.
For php 5.3 you can do:
include_once(__DIR__ . '/../untitled/sanitize_string.php');
where __DIR__ is the directory for the current file
For older versions you can use
include_once(dirname(__FILE__) . '/../untitled/sanitize_string.php');
where __FILE__ is the path for the current file
Lets say you have the following structure:
/app/path/public/header.php
/app/path/public/user/profile.php
/app/path/untitled/sanitize_string.php
If your header.php includes santitize_script.php with a relative path like so:
include_once("../untitled/sanitize_string.php");
the php interpreter will try to include that file RELATIVELY to the current working dir so if you will do a request like http://localhost/header.php it will try to include it from /app/path/public/../untitled/sanitize_string.php and it will work.
But if you will try to do a request like http://localhost/user/profile.php and profile.php includes header.php, than header.php will try to include the file from /app/path/public/user/../untitled/sanitize_string.php and this will not work anymore. (/app/path/public/user beeing the current working dir now)
That's why is a good practice to use absolute paths when including files. When used in header.php, the __DIR__ and __FILE__ constants will always have the same values: /app/path/public and /app/path/public/header.php respectively no matter where header.php will be used thereafter
Use absolute path...
include_once('/home/you/www/include.php');
Use absolute path as yes123 said.
include_once(dirname(__FILE__)."/../untitled/sanitize_string.php");
You're going to have to use absolute paths here, as opposed to relative. I often set up some constants to represent important directories, using the old Server vars. Like so:
define('MY_DIR',$_SERVER['DOCUMENT_ROOT'].'/path/to/yer/dir');
Then, modify your include statement:
include_once(MY_DIR.'/your_file.php');
Related
I have inside my div a php require to
require('inc/coreturni/list.php');
that requires again a db connection
require('inc/connect.inc.php');
and they both work when I load the page.
but if I try to refresh the div, by refreshing the first include file after a click event, with jquery load()
I got the following path error:
Warning: require(inc/connect.inc.php): failed to open stream: No such file or directory in C:\xampp\htdocs\taximat\inc\coreturni\list.inc.php on line 2
Fatal error: require(): Failed opening required 'inc/connect.inc.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\taximat\inc\coreturni\list.inc.php on line 2
why the path should be different? how could i fix it?
Your error explains it all:
Warning: require(inc/connect.inc.php): failed to open stream: No such
file or directory in
C:\xampp\htdocs\taximat\inc\coreturni\list.inc.php on line 2
Fatal error: require(): Failed opening required 'inc/connect.inc.php'
(include_path='.;C:\xampp\php\PEAR') in
C:\xampp\htdocs\taximat\inc\coreturni\list.inc.php on line 2
Notice how it says it can’t find inc/connect.inc.php in this directory:
C:\xampp\htdocs\taximat\inc\coreturni
It’s because your require statements are referring to relative paths. So when the file list.inc.php is loaded, the require is assuming that inc/connect.inc.php is located in C:\xampp\htdocs\taximat\inc\coreturni. Which is why it is failing.
You should set a base path in your app like so. Just a note that I am unclear on forward-slash or back-slash syntax in a case like this since I work mainly on Mac & Unix systems and the error shows windows paths, but the PHP is showing Unix paths.
$BASE_PATH='/xampp/htdocs/taximat/inc/coreturni/';
And the require all files like this:
require($BASE_PATH . 'inc/coreturni/list.php');
And like this:
require($BASE_PATH . 'inc/connect.inc.php');
That way no matter where the files are called from, the require will use an absolute path to the file. And since $BASE_PATH is a one-time variable you set you can just set it in your main config file and not worry about it. To make the code portable, just change $BASE_PATH based on system setups.
When the page is displayed for the first time, your paths are like this:
page.php
inc/
coreturni/
list.php
connect.php
In other words, all require statements will be relative as seen from page.php.
However, when you request list.php directly using AJAX, it looks more like this:
list.php
../
connect.php
Now, the require should have been for ../connect.php.
Solution
One way to solve this is by calculating the absolute path based on the file currently being processed (i.e. list.php) like this:
require dirname(__DIR__) . '/connect.php';
The dirname(__DIR__) construct will always yield the directory on your file system that's one higher up than the current script is in.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: Why such weird behaviour when I include a file from another directory which also includes a file?
I have a problem including a file that has an included file.
core.inc.php PATH: www/test/includes/core.inc.php
included file in core.inc.php : include_once ('../../connectdb.php');
connectdb.php PATH: www/connectdb.php
index.php PATH: www/test/index.php
included file in index.php : include_once ('included/core.inc.php');
When I run index.php the following warnings are popping up:
(!) Warning: include_once(../../connectdb.php): failed to open stream:
No such file or directory in G:\wamp\www\test\includes\core.inc.php on line 7
(!) Warning: include_once(): Failed opening '../../connectdb.php' for
inclusion (include_path='.;C:\php\pear') in G:\wamp\www\test\includes\core.inc.php on line 7
In order to dinamically change the included paths what is the best practice? Please help me on my problem. Thank you.
To avoid such problems, you may use PHP magic constant __DIR__ which will be replaced by current file's directory.
For example:
include_once(__DIR__ . '/relative/path/to/file-to-include.php');
Note that __DIR__ is only available for PHP 5.3+. Below that version you can replace __DIR__ by dirname(__FILE__)
BTW, autoloading is a good practice to avoid includes mess.
included file in index.php : include_once ('./includes/core.inc.php');
Your relative path in index.php pointing to the core.inc.php is simply wrong.
You are saying: "From the location of index.php, on directory up, and there it is." But there is no file "core.inc.php" in the "www" directory.
The correct path is "includes/core.inc.php": From the current directory "www/test" one directory up into "includes" and there it is.
Any mentions of using __DIR__ or __FILE__ magic constants will also work, but this is a different approach: It will create an absolute path to the file by using relative additions. It works the same, but looks more complicated.
If you are using classes, try to implement autoloading. Then you do not need to include files explicitly, you simply use the classes.
I have this condition :
a file : /public_html/folderX/test.php has a line : require_once '../functions/sendemail.php'
on the other hand, /public_html/functions/sendemail.php has a line : require_once '../config.php'
config.php loads perfectly in this situation.
the problem occurs when I try to add that functions/sendemail.php on file(s) which not in the folderX, for example :
when I tried to add require_once 'functions/sendemail.php' on public_html/test.php I got this error message :
Warning: require_once(../config-min.php) [function.require-once]: failed to open stream: No such file or directory in public_html/test.php
how to make require_once '../config.php' inside functions/sendemail.php works 'independently' so wherever it's included on any files this 'require_once' problem won't occur anymore.
I tried to change into 'include_once' but still doesn't work.
thanks!
try something like
require_once( dirname(__FILE__).'/../config.php')
Try using __DIR__ to attain the current path of the script.
require_once(__DIR__.'../config.php');
__DIR__ only works on php 5.3
__DIR__
The directory of the file. If used inside an include, the directory of
the included file is returned. This is equivalent to dirname(__FILE__).
This directory name does not have a trailing slash unless it is the root directory.
(Added in PHP 5.3.0.)
I believe the relative path names are biting you here. Relative paths are (to my knowledge) based on the directory of the currently active script. PHP doesn't chdir into a folder when including or requiring files. The best recommendation (in my limited experience) for this kind of thing is to use absolute paths where possible. So something like:
require_once('../config.php');
would become:
require_once('/home/myuser/config.php'); // Or wherever the file really is
The dirname function can help in this situation.
You must understand that PHP changes directory to that of the outermost script. When you use relative paths (e.g. those that begin with ./, ../, or those that do not begin with /), PHP will use the current directory to resolve the relative paths. This causes problem when you copy-paste the include lines in your code. Consider this directory structure:
/index.php
/admin/index.php
/lib/include.php
Assume the two index files contain these lines:
include_once("lib/include.php");
The above line will work when /index.php is called but not when /admin/index.php is called.
The solution is to not copy-paste code, use correct relative file paths in your include calls:
/index.php -> include_once("lib/include.php");
/admin/index.php -> include_once("../lib/include.php");
i'm getting following error in my PHP file.
Warning: include(../config/config.php) [function.include]: failed to open stream: No
such file or directory in C:\xampp\htdocs\my-proj\functions\function.php on line 2
let me describe my folder structure
ROOT folder /index.php
functions / function.php
config / config.php
signup / signup.php
now, If i use absolute path, then it is give the same error in signup.php, and if I use relative path then it is giving this error in index.php
any help would be appreciated.
use
include("$_SERVER[DOCUMENT_ROOT]/config/config.php");
The file paths are relative to the invoked script. If your application gets invoked by http requests to index.php, then the include() path needs to be relative to that - even if the include statement itself is located in the functions.php script.
A common workaround is to make all paths absolute in relation to the document root:
include("$_SERVER[DOCUMENT_ROOT]/config/config.php");
// Note: leaving out array keys only valid in double quote string context.
That would work in index.php and functions.php alike.
Use include __DIR__."/../config/config.php"; if you want to include a file relative to the file you're currently executing in. If you're using a version of php older than 5.3.0 (you shouldn't), replace __DIR__ with dirname(__FILE__).
$_SERVER['DOCUMENT_ROOT'] is not set when using commandline and requires that your project is relative to the DOCUMENT_ROOT instead of allowing the user to place it wherever they please. If phpMyAdmin used this variable, you would be forced to accommodate it instead of just placing it wherever you want. That's another thing, it's a variable, so there's a potential security issue too.
If config.php is necessary, I suggest using require instead, otherwise use if (file_exists($file)) {require $file;} so you can avoid warnings when it doesn't exist and get an error when it can't be read (I assume if it exists, it's intended to be used).
I need to include one PHP file into another. The PHP file that needs to be included sits in a separate directory though. This is how it is set up:
folder1/global-functions.php
folder1/folder2/functions.php
I need to include the 'global-functions.php' in the 'functions.php'
I tried:
<?php include("../global-functions.php"); ?>
But this will not work. It returns this error message:
Warning: include(../global-functions.php) [function.include]: failed to open stream: No such file or directory in /home/user/public_html/wp-content/themes/folder1/folder2/custom_functions.php on line 2
Warning: include() [function.include]: Failed opening '../global-functions.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/user/public_html/wp-content/themes/folder1/folder2/custom_functions.php on line 2
Try including the file with an absolute path: something like this:
<?php include ($_SERVER['DOCUMENT_ROOT']."/folder1/global-functions.php");?>
Your original include fails because... the relative path in your include is relative to the current directory, which in your case is not "folder1/folder2/". The current directory is likely to be the page from which you are serving your content.
You need to either use an absolute path (with the help of $_SERVER['DOCUMENT_ROOT'] as in #Coomie's answer) or change your include_path to include the location of your included files (but then you must not use a relative path, but you wouldn't need to anyway).
You are including functions.php in itself. Change functions.php to global-functions.php.
And just out of curiosity, why have different files for functions? Why not make classes and objects and make your life easier?