I did a lot of reading but I cannot answer one question I have with php include behavior.
On php.net it says that it looks into the directories that are in include_path variable. After that it looks in the current directory. If I put relative path (starting with dot) or absolute path it ignores the include_path.
So far so good.
I get confused when I see examples on the internet that start with something like that:
include('LibName/SomeFile.php');
Would php take every path from include_path and append 'LibName/SomeFile.php to look for the file? What is the behavior?
It's exactly what you said it is. It checks each directory in include_path to see if 'LibName/SomeFile.php' references a file relative to those paths. If not, it tries the current directory.
PHP considers each entry in the include path separately when looking for files to include. It will check the first path, and if it doesn't find it, check the next path, until it either locates the included file or returns with a warning or an error. You may modify or set your include path at runtime using set_include_path().
You can find more info here: http://www.php.net/manual/en/ini.core.php#ini.include-path
Related
If I have an empty include_path in PHP (like include_path=:), my include and require functions still work. It is like include tries the initial script's directory first, regardless of what your include_path is set to.
This gets more confusing to me when I set the include_path to something like /usr/lib/php5/, and then try to include('test.php') from a script that has no test.php in the same directory. I would imagine it would look for test.php in my specified include_path if it wasn't present in the current directory.
I am not looking for a solution; I can already solve my current problem. I am looking for an explanation as to how include_path works. Thanks!
basically if you just speicfy the name of the file to be included without any path info then then include_path will be used.
i.e.
include 'wow.php'; (this will use include_path to try to find it)
if it cant find it then it will look in the same directory as the file that tried to include it.
include '/blah/wow.php';
because this has some path info in it then it will ignore include_path all together.
Use this
include($_SERVER['DOCUMENT_ROOT'].'/test.php');
I'm sure I'm just being a bit stupid, but the include_path docs seem to be a bit confusing; what is
Using a . in the include path allows for relative includes as it means the
current directory. However, it is more efficient to explicitly use include
'./file' than having PHP always check the current directory for every include.
actually trying to say?
what is
Using a . in the include path allows for relative includes as it means the
current directory. However, it is more efficient to explicitly use include
'./file' than having PHP always check the current directory for every include.
actually trying to say?
Assume you want to use include_path to search in folder /somepath/foo/includes for include files when you do not specify the path explicitly. You may have most of your include files laying there, so that should be not too inefficient.
Now you are including baz.php, xzy.php and 123.php from that directory – and if you have given . first in the include_path, then PHP will search the current directory first always, although in most cases the file will be located in the /somepath/foo/includes folder.
If you don’t specify . at the beginning of your include path and use ./abc.php explicitly when you are including a file from the current directory (special case, not one of your “normal” includes), then for all other of your include files that you just include using baz.php, xzy.php and 123.php, PHP will not have to search the current directory first – which is useless, since those files are located in your special include folder; so PHP can go look there in the first place.
It is talking about dot at the begining in the include_path.
Form PHP Documentation : If filename begins with ./ or ../, it is looked only in the current working directory.
./ is the current directory. It is largely the same as just someFile.php.In many cases \it doesn't check any standard places PHP might look for a file, instead checking only the current directory.
Edited. 'more efficient' means that it better to set path in such way with dot. In other way it will try to find not only in the current working directory. So, it can be a problem if you want to include file in the some directory, but the file is not exist, it will try to find in other path, and it can take some time and make wrong include or just an error.
I have this site I'm making. When I include a php file from the same folder it works but when I do
<?php include('/ioanblog/appstore/header.php');?>
it doesn't work or
<?php include('http://www.domain.co.uk/appstore/header.php');?>
that isn't working either.
All the header is holding is a style sheet and Piwik code, it will also hold navigation.
It's hard to say why the include won't work without knowing the folder structure.
But you can try it with the absolute path: /home/user/domain/public_html/etc...
Read the documentation on relative paths.
If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.
You are using /ioanblog/… which is an absolute path. I suspect your site is in /var/www/public_html/ioanblog/… or /home/user/ioanblog/… or something similar, not in /ioanblog/…. You should remove the first / making it a relative instead of an absolute path. You might have to go "up" to parent directories like ../contents/header.php or ../../contents/header.php for the actual file.
As pointed out by brbcoding it should probably be ../../header.php. But first make sure you understand absolute/relative paths!
You might also want to read up on realpath() to 'convert' relative paths to absolute paths.
Use <?php include('../../header.php');?>
That's my guess... You don't have an appstore directory anywhere as far as I can tell.
EDIT: Updated with the correct path... Coming from the libreoffice directory.
try removing the first slash so include('ioanblog/appstore/header.php');
I have a directory/file tree as follows:
index.php
/frame/main_class.php
/frame/func/function_1.php
/frame/func/function_1.php
/cfg/config.php
//index.php
require('frame/main_class.php');
new main_class;
//frame/main_class.php
class main_class{
public function __construct(){
require('func/function_1.php');
require('func/function_2.php');
require('cfg/config.php');
}
}
The weird part is that it works. Maybe it is late and I am having a dumb-moment, but shouldn't "require('cfg/config');" be written "require('../cfg/config.php');" ?
And if it is using the root of index.php, then "require('func/function_1.php');" shouldn't work, right?
I have quadruple checked the remote server thinking that maybe there was a stray file or two... there isn't.
How can the two require statements have a different base path.....?
Does anyone know of a code snippet that could cause this to happen? I am working with some $_SERVER variables but I don't appear to be changing any of them....!?
"Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing." Explicitly saying include dirname(__FILE__) . '/path/to/file.php';avoids this confusion. – DCoder
Link to PHP Manual on "dirname".
The PHP engine will look for the requested files in the current directory, but it will also look for them in the list of paths defined in INCLUDE_PATH. If the include path lists the path from where your script is running then the given code will work. If not then it wont.
For that reason amongst others it's not a good idea to rely on the include path to resolve the path of included files. You should give the full path instead.
if I call php's parse_ini_file("foo.ini"), in what paths does it look for foo.ini ?
the include path? the function's documentation doesn't mention it.
The filename argument for parse_ini_file is a standard php filename, so the same rules will apply as opening a file using fopen.
You must either specify an absolute file path ("/path/to/my.ini") or a path relative to your current working directory ("my.ini"). See getcwd for your current working directory.
Unlike the default fopen command, if a relative path is specified ("my.ini") parse_ini_file will search include paths after searching your current working directory. I verified this in php 5.2.6.
I would imagine it only looks in the current working directory - See http://uk3.php.net/manual/en/function.getcwd.php if you want to know what that is.
You can always find a path relative to your application by basing it on $_SERVER['DOCUMENT_ROOT']
It could depend on your config but mostly it's current directory and then include_path