S 0en dirname of s 3en__ get the absolute path of the current file

  • 2020-05-09 18:14:56
  • OfStack

dirname PHP constants (__file__)

S 6en__ : called the PHP magic constant, returns the full path and file name of the current PHP script, with 1 absolute path

1) dirname(s) (s) 12en__) function returns the path in which the script is located.

For example, b.php contains the following:


<?php 
$basedir = dirname(__FILE__); 
echo $basedir
// Will be printed on the page 1 The absolute path of the file ! 
?> 

I do test results: E: / website/othertest/cms

This is equivalent to the use of server in asp.mappth

If b.php is referenced by require or include in another directory, a.php. The contents of the variable $basedir are still the path to the folder where b.php resides. Instead of becoming the directory where the a.php files reside.

2) dirname(s) (s) 45en__) 1 will return the current directory of the file to the system root directory structure.

The current file name is not returned. dirname(s 49en__) may also return 1. (current directory) [because b.php files are in the http.conf or PHP configuration development environment's default WEB directory

Several magic constants of PHP

The name of the show
S/s current line number in the file with s/s 62en__.
S 64en__ full path and file name of the file. If used in an include file, the include file name is returned. Starting with PHP 4.0.2, s 66en__ will always contain 1 absolute path, while previous versions will sometimes contain 1 relative path.
S 68en__ function name (PHP 4.3.0). This constant returns the name (case sensitive) of the function when it was defined from PHP 5. In PHP 4 the value is always lowercase.
Name of class s (PHP 4.3.0 plus). This constant returns the name (case sensitive) of the class when it was defined from PHP 5. In PHP 4 the value is always lowercase.
Method name of class s (PHP 5.0.0). Returns the name of the method when it was defined (case sensitive if the current file is under (d:\www\) and the file name is test.php.
The test code is as follows:


<?php 
echo __FILE__ ; //  Get the absolute address of the current file, and the result: D:\www\test.php 
echo dirname(__FILE__); //  Get the absolute directory where the current file is located, and the result: D:\www\ 
echo dirname(dirname(__FILE__)); // Gets the top of the current file 1 Layer directory name, result: D:\ 
?> 

Use method tips,
S = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s
If repeated once, you can move the directory up one level:
For example, $d = dirname(dirname(s) 96en__);
You're just giving one directory to dirname() as a parameter. Because dirname() returns the last directory without \\ or /
So when you reuse it, think of dirname() as treating the lowest level of the directory as the file name. Return as usual
The parent directory of the current directory. This is repeated to get its upper level 1 directory.

Contains the files that get the directory at the previous level
include(dirname(__FILE__).'/../filename.php');

The path of s 109en__ is the file in which the current code is located

dirname (dirname (__FILE__)); You get the directory name 1 layer above the file

dirname (__FILE__); You get the name of the directory in which the file is located


Related articles: