Summary of Three Functions of PHP Parsing Directory Path

  • 2021-08-03 09:32:55
  • OfStack

To describe the location of a file, you can use the alignment path and the relative path. The absolute path starts at the root level by level into each subdirectory, and finally specifies the file name or directory name. The relative directory enters a directory from the current directory, and finally specifies the file name or directory name. Under each directory of the system, there are two special directories, "." and "..", which indicate the current directory and the parent directory of the current directory (the upper level 1 directory), respectively. For example:


$unixPath = " /var/www/html/index.php " ; In UNIX Absolute path in the system, you must use " / "As a path separator
$winPath = " C:\\Appserv\\www\\index.php " ; In fact, in fact, the Windows The absolute path of the system, using the " \ "As a path separator
$winPath = " C:/Appserv/www/index.php " ; In Windows It is also accepted in the system. " / "As a path separator, it is recommended to use
$fileName1= " file.txt " ; Relative path, in the current directory file.txt Documents
$fileName2= " javascript/common.js " ; Relative path, in the current directory javascript Subdirectory common.js Documents
$fileName3= " ../images/logo.gif " ; Relative path, up 1 Level directory images Subdirectory logo.gif Documents

In the above example, the formats of absolute path and relative path in UNIX and Windows systems are listed respectively. Among them, the forward slash "/" must be used as the path separator in UNIX system, while the backslash "\" is used as the path separator by default in Windows system. In the program, it is indicated that "\" should be escaped, but the forward slash "/" is also accepted as the separator. In order to have a good portability of the program, it is recommended to use "/" as the path separator of the file. Alternatively, you can use the built-in constant DIRECTORY_SEPARATOR of PHP, which is the default file path separator for the current operating system. For example:


$fileName2 = " javascript " .DIRECTORY_SEPARATOR. " common.js " ; In fact, in fact, the Unix For " / ", Windows For " \ "

It is often useful to separate the attributes in the directory path, such as the extension at the end, the directory part, and the base name. These tasks can be accomplished through the PHP system functions basename (), dirname (), and pathinfo ().

① Function basename ()

The function basename () returns the filename portion of the path. The prototype of this function is as follows:


string basename(string path[,string suffix]) // Returns the filename portion of the path

This function gives a string containing its full path to a file. This function returns the basic file name. The second parameter is optional and specifies the extension of the file. If provided, this extension will not be output. This function is used as shown in the following code:

<?php
// Contains a point 1 A string of all paths to files
$path = "/var/www/html/page.php";
// Displays the file name with the file extension, and outputs page.php
echo basename($path);
// Displays file names without file extensions, and outputs page
echo basename($path,".php");
?>

② Function dirname ()

This function is just the opposite of basename (). It only needs one parameter and gives a string containing all the paths to a file. This function returns the directory name after removing the file name. This function is used as shown in the following code:


<?php
$path = "/var/www/html/page.php";
echo dirname($path); // Returns directory name /var/www/html
echo dirname('c:/'); // Returns directory name c:/
?>

③ Function pathinfo ()

The function pathinfo () returns an associative array that includes the directory name, base name, and extension in the specified path. Referenced by array keys dirname, basename, and extension, respectively. The use of this function is shown in the following code.


<?php
$path = "/var/www/html/page.php";
$path_parts = pathinfo($path); // Returns an associative array that includes the directory name, base name, and extension in the specified path
echo $path_parts["dirname"]; // Output directory name /var/www/html
echo $path_parts["basename"]; // Output base name page.php
echo $path_parts["extension"]; // Output extension .php
?>


Related articles: