As of PHP 5.3.0, you can use __DIR__ as a replacement for dirname(__FILE__)
(PHP 4, PHP 5, PHP 7, PHP 8)
dirname — Returns a parent directory's path
Given a string containing the path of a file or directory, this function
will return the parent directory's path that is
levels
up from the current directory.
Note:
dirname() operates naively on the input string, and is not aware of the actual filesystem, or path components such as "
..
".
On Windows, dirname() assumes the currently set codepage, so for it to see the
correct directory name with multibyte character paths, the matching codepage must
be set.
If path
contains characters which are invalid for the
current codepage, the behavior of dirname() is undefined.
On other systems, dirname() assumes path
to be encoded in an ASCII compatible encoding. Otherwise the behavior of the
the function is undefined.
path
A path.
On Windows, both slash (/
) and backslash
(\
) are used as directory separator character. In
other environments, it is the forward slash (/
).
levels
The number of parent directories to go up.
This must be an integer greater than 0.
Returns the path of a parent directory. If there are no slashes in
path
, a dot ('.
') is returned,
indicating the current directory. Otherwise, the returned string is
path
with any trailing
/component
removed.
Be careful when using this function in a loop that can reach the top-level directory as this can result in an infinite loop.
<?php
dirname('.'); // Will return '.'.
dirname('/'); // Will return `\` on Windows and '/' on *nix systems.
dirname('\\'); // Will return `\` on Windows and '.' on *nix systems.
dirname('C:\\'); // Will return 'C:\' on Windows and '.' on *nix systems.
?>
Version | Description |
---|---|
7.0.0 |
Added the optional levels parameter.
|
Example #1 dirname() example
<?php
echo dirname("/etc/passwd") . PHP_EOL;
echo dirname("/etc/") . PHP_EOL;
echo dirname(".") . PHP_EOL;
echo dirname("C:\\") . PHP_EOL;
echo dirname("/usr/local/lib", 2);
The above example will output something similar to:
/etc / (or \ on Windows) . C:\ /usr
As of PHP 5.3.0, you can use __DIR__ as a replacement for dirname(__FILE__)
Since the paths in the examples given only have two parts (e.g. "/etc/passwd") it is not obvious whether dirname returns the single path element of the parent directory or whether it returns the whole path up to and including the parent directory. From experimentation it appears to be the latter.
e.g.
dirname('/usr/local/magic/bin');
returns '/usr/local/magic' and not just 'magic'
Also it is not immediately obvious that dirname effectively returns the parent directory of the last item of the path regardless of whether the last item is a directory or a file. (i.e. one might think that if the path given was a directory then dirname would return the entire original path since that is a directory name.)
Further the presense of a directory separator at the end of the path does not necessarily indicate that last item of the path is a directory, and so
dirname('/usr/local/magic/bin/'); #note final '/'
would return the same result as in my example above.
In short this seems to be more of a string manipulation function that strips off the last non-null file or directory element off of a path string.
To get the directory of current included file:
<?php
dirname(__FILE__);
?>
For example, if a script called 'database.init.php' which is included from anywhere on the filesystem wants to include the script 'database.class.php', which lays in the same directory, you can use:
<?php
include_once(dirname(__FILE__) . '/database.class.php');
?>
Be aware that if you call dirname(__FILE__) on Windows, you may get backslashes. If you then try to use str_replace() or preg_replace() to replace part of the path using forward slashes in your search pattern, there will be no match. You can normalize paths with $path = str_replace('\\', '/' ,$path) before doing any transformations
The dirname function does not usually return a slash on the end, which might encourage you to create links using code like this:
$url = dirname($_SERVER['PHP_SELF']) . '/somepage.php';
However dirname returns a slash if the path you specify is the root, so $url in that case would become '//somepage.php'. If you put that URL as the action on a form, for example, submitting the form will try to go to http://somepage.php.
I ran into this when I wrote a site on a url with a path, www.somehost.com/client/somepage.php, where the code above works great, but then wanted to put it on a subdomain, client.somehost.com/somepage.php, where things started breaking.
The best solution would be to create a function that generates absolute URLs and use that throughout the site, but creating a safe_dirname function (and an htaccess rewrite to fix double-slashes just in case) fixed the issue for me:
<?php
function safe_dirname($path)
{
$dirname = dirname($path);
return $dirname == '/' ? '' : $dirname;
}
?>
Expanding on Anonymous' comment, this is not necessarily correct. If the user is using a secure protocol, this URL is inaccurate. This will work properly:
<?php
// Is the user using HTTPS?
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')) ? 'https://' : 'http://';
// Complete the URL
$url .= $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// echo the URL
echo $url;
?>
Attention with this. Dirname likes to mess with the slashes.
On Windows, Apache:
<?php
echo '$_SERVER[PHP_SELF]: ' . $_SERVER['PHP_SELF'] . '<br />';
echo 'Dirname($_SERVER[PHP_SELF]: ' . dirname($_SERVER['PHP_SELF']) . '<br>';
?>
prints out
$_SERVER[PHP_SELF]: /index.php
Dirname($_SERVER[PHP_SELF]: \
As usual, to include or require a file, we use this
<?php
require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'my_file.php';
?>
in rare case, we have current file existing at the root directory, dirname would return C:\ or / , then the line above contains 2 slashes \\ or //
To handle this this case, we use rtrim to clear slashes.
<?php
require rtrim(dirname(__FILE__), '/\\') . DIRECTORY_SEPARATOR . 'my_file.php';
?>
Also, another use of dirname is to get virtual directory (url path), the issue is the same as above, we have to check and process before concatenating strings
Inside of script.php I needed to know the name of the containing directory. For example, if my script was in '/var/www/htdocs/website/somedir/script.php' i needed to know 'somedir' in a unified way.
The solution is:
<?php
$containing_dir = basename(dirname(__FILE__));
?>
File located locally in: F:\localhost\www\Shaz3e-ResponsiveFramework\S3-CMS\_source
Localhost Path: http://s3lab.com/Shaz3e-ResponsiveFramework/S3-CMS/_source/
Example 1: dirname($_SERVER['PHP_SELF']); //output: /Shaz3e-ResponsiveFramework/S3-CMS/_source
Example 2: "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']); //output: http://s3lab.com/Shaz3e-ResponsiveFramework/S3-CMS/_source
Example 3: $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); //output: s3lab.com/Shaz3e-ResponsiveFramework/S3-CMS/_source
The same function but a bit improved, will use REQUEST_URI, if not available, will use PHP_SELF and if not available will use __FILE__, in this case, the function MUST be in the same file. It should work, both under Windows and *NIX.
<?php
function my_dir(){
return end(explode('/', dirname(!empty($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : !empty($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : str_replace('\\','/',__FILE__))));
}
?>
You can use it to get parent directory:
dirname(dirname(__FILE__))
...include a file relative to file path:
include(dirname(__FILE__) . '/path/relative/file_to_include.php');
..etc.
what about a recursive dirname. To get $count levels up in a directory.
<?php
function r_dirname($path, $count=1){
if ($count > 1){
return dirname(r_dirname($path, --$count));
}else{
return dirname($path);
}
}
In some situations (I can't locate the dependencies) basename and dirname may return incorrect values if parsed string is in UTF-8.
Like, dirname("glossary/задний-фокус") will return "glossary" and basename("glossary/задний-фокус") will return "-фокус".
Quickfix is
str_replace("!$!", "", dirname(str_replace("/", "!$!/!$!", $q)))
A simple way to show the www path to a folder containing a file...
echo "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
The best way to get the absolute path of the folder of the currently parsed PHP script is:
<?php
if (DIRECTORY_SEPARATOR=='/')
$absolute_path = dirname(__FILE__).'/';
else
$absolute_path = str_replace('\\', '/', dirname(__FILE__)).'/';
?>
This will result in an absolute unix-style path which works ok also on PHP5 under Windows, where mixing '\' and '/' may give troubles.
[EDIT by danbrown AT php DOT net: Applied author-supplied fix from follow-up note.]
Getting absolute path of the current script:
<?php
dirname(__FILE__)
?>
Getting webserver relative path of the current script...
<?php
function GetRelativePath($path)
{
$npath = str_replace('\\', '/', $path);
return str_replace(GetVar('DOCUMENT_ROOT'), '', $npath);
}
?>
later on
<?php
GetRelativePath(dirname(__FILE__));
?>
If anyone has a better way, get to the constructive critisism!
dirname can be used to create self referencing web scripts with the following one liner.
<?php
$base_url = str_replace($DOCUMENT_ROOT, "", dirname($PHP_SELF));
?>
Using this method on a file such as:
/home/mysite/public_html/wherever/whatever.php
will return:
/wherever
Now $base_url can be used in your HTML to reference other scripts in the same directory.
Example:
href='<?=$base_url?>/myscript.php'
In my mvc based framework i make BASE_PATH and BASE_URL definitions like the following and both work well in the framework without problem.
index.php :
define('BASE_PATH',realpath('.'));
define('BASE_URL', dirname($_SERVER["SCRIPT_NAME"]));
BASE_PATH is for server side inclusions.
BASE_URL is for client side inclusions (scripts, css files, images etc.)
Most mkpath() function I saw listed here seem long and convoluted.
Here's mine:
<?php
function mkpath($path)
{
if(@mkdir($path) or file_exists($path)) return true;
return (mkpath(dirname($path)) and mkdir($path));
}
?>
Untested on windows, but dirname() manual says it should work.