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
Related
I have several instances of php includes on some of my pages (usual things like footer markup and headers etc) and Id rather use an absolute php path rather than the ../../ relative path I am using.
I thought this would work:
<?php include
include(dirname(__FILE__) . "/dir/script_name.php");
?>
my question is: is the /dir/script_name.php - the exact full path, and if so, what is the point of __FILE__ ?
The function dirname() removes the file name from the path and gives us the absolute path of the directory the file is in - right? So why bother with the function dirname() if I am already giving the full path - ?
Hope that makes sense
__FILE__ is the php magic variable which is the full path of the currently executing file (where it appears), dirname() removes the file name from it, thereby giving you the full path of the folder the current script is in.
It is really just a hack, as another magic variable __DIR__ (the directory of the currently executing script) was only added in 5.3 and therefore the dirname(__FILE__) was the easiest way to achieve the same thing in earlier versions.
The function dirname() removes the file name from the path and gives us the absolute path of the directory the file is in - right?
Yes, this is correct. The path you can use could be relative or the full system path.
So why bother with the function dirname() if I am already giving the full path - ?
If you are hard coding the full file system path then your application will become less portable; Your webroot directory would be different from mine. This is normally why you will see code with the path being generated like this.
I would personally look into the PHP autoloader as a much more flexible alternative to managing class includes.
It all depends on what you want. In the strictest sense, the path you're describing is "relative" to the file you're executing. For a true absolute path, just make sure the filename starts with a "/".
like so:
include "/path/to/file/script.php";
This absolute path successfully includes my file httpapi.inc.php:
require_once '/home/devel/wwwroot/nm/dev/http-api/http-api/src/httpapi.inc.php';
The calling file is:
Caller relative:
/devel/phi/dev/appcenter-head/appcenter/application/nm/index.php
Caller location:
/home/devel/wwwroot/phi/dev/appcenter-head/appcenter/application/nm
Now, how can I include that same file based on a relative path like:
/devel/nm/dev/http-api/http-api/index.php
I must use this path "as is", since it is a config option passed by the user.
require() and include() should operate relative to the configured include paths and the currently executing script. So, this should work:
require_once('src/httpapi.inc.php');
To be perfectly precise, though, the current script's working directly is actually designated as one of the include paths in the default configuration. It won't work if you've mucked with it.
Using a . in the include path allows for relative includes as it means
the current directory.
Regarding your latest edit, your paths are different enough that it's simpler to just use the full path or add /home/devel/wwwroot/nm/dev/http-api/http-api/ to your config and use require_once('src/httpapi.inc.php').
You might use the document root from the $_SERVER variable:
http://www.php.net/manual/en/reserved.variables.server.php
require_once $_SERVER['DOCUMENT_ROOT'] . '/nm/dev/http-api/http-api/src/httpapi.inc.php';
include "src/httpapi.inc.php";
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');
This works
<?php include("inc/c.php")?>
But in a folder past this, this does not work
<?php include("../inc/c.php")?>
I have to do
<?php include("/var/web/public_html/etc/inc/c.php")?>
I know in ASP you can enable virtual paths and directories. Is this the same with PHP?
If you're including a file from a folder, all includes are relative to the includer's file.
Therefore, the same code should work for the file in the sub-folder:
<?php include("inc/c.php")?>
You can use realpath(dirname(__FILE__)) to include files relatively to current file:
include(realpath(dirname(__FILE__).'/../inc/c.php'));
You can add directories to PHP's include_path directory. When you specify a relative file name, PHP will look for that file relative to all directories specified in the include_path.
Take a look at http://php.net/manual/en/function.set-include-path.php#example-488.
use
dirname(__file__)
it will return you path of current directory.
But in a folder past this, this does not work
Nope, it does work.
I know in ASP you can enable virtual paths and directories. Is this the same with PHP?
Yes. But whole virtual path thing has nothing to do with your case.
This is not PHP problem. This is developer's problem who is using wrong path.
To make your code fool-proof, always use absolute paths. Build paths not from current location but from the site root. So, it will be all the same in the ANY page on your site.
Most general way would be
include $_SERVER['DOCUMENT_ROOT']."/etc/inc/c.php";
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