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.
Related
I have been inspecting code, reading questions on StackOverflow, but I simply don't get it, or rather I dont understand the explanations and / or logic behind it.
Consider The Following
If I have a directory structure like this
Now I want to set the head.php file to be globally accessible throughout the application (just as an example)
define('Head', __DIR__ .'/views/head.php');
If I do the above, I get the following result:
C:\xampp\htdocs\carRental/views/head.php"
Which is technically what I want,, however, notice the URL contains forward and backslashes?
Can I get access to the head.php file by calling Head anywhere in my directory tree?
Im sorry, Ive been inspecting code and read the manual and questions on here, if anyone could give a rookie a clear explanation it would be greatly appreciated.
UPDATE:
When I try to do the following in landingPage.php I get the following errors
include_once Head;
Notice: Use of undefined constant Head - assumed 'Head' in
C:\xampp\htdocs\carRental\views\landingPage.php on line 2
Warning: include_once(Head): failed to open stream: No such file or
directory in C:\xampp\htdocs\carRental\views\landingPage.php on line 2
Warning: include_once(): Failed opening 'Head' for inclusion
(include_path='C:\xampp\php\PEAR') in
C:\xampp\htdocs\carRental\views\landingPage.php on line 2
When you used define('Head', __DIR__ .'/views/head.php'); you have hardcoded the slashes in the definition.
However windows by default uses \ as the default directory separator so __DIR__ will be using \ in the path when in Windows (it's ok with using / as an alternative one though so it shouldn't be a problem).
You can do the following if you want them to be consistent:
define('Head', __DIR__ .DIRECTORY_SEPARATOR ."views".DIRECTORY_SEPARATOR ."head.php");
Which is technically what I want,, however, notice the URL contains forward and backslashes?
yes
Can I get access to the head.php file by calling Head anywhere in my directory tree?
no
The mix of foreward and backward slashes is created by your command
__DIR__ creates C:\xampp\htdocs\carRental and '/views/head.php' is the string you append.
To be able to use your defined HEAD you would have to load the php file which defines it. another php files does not know what this file does or doesn't do as long as it is not persisted. (which you don't do in the code provided)
To load a file and make your definition available use include_once / require_once
__DIR__ will always resolve to the absolute directory of the file using it.
The reason for forward and backslashes. This part:
C:\xampp\htdocs\
Is Windows file path.
This part:
carRental/views/head.php
Is the webserver path, i.e not Windows.
Your define will hold the correct file path so try to include it now:
include_once Head;
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 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');
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?