3 Functions of PHP Parsing directory path Summary _php tips

Source: Internet
Author: User
Tags parent directory

To describe the location of a file, you can use a summary path and a relative path. An absolute path is a first-level entry from the root to each subdirectory, and the file name or directory name is finally specified. The relative directory enters a directory from the current directory, and the file name or directory name is finally specified. There are two special directories in each directory of the system. and "..", respectively, indicating the parent directory of the current directory and the current directory (the previous level directory). For example:

Copy Code code as follows:

$unixPath = "/var/www/html/index.php"; – Absolute path in Unix system, must use "/" as the path separator
$winPath = "c:\\appserv\\www\\index.php"; Beginning – Windows The absolute path of the system, using "\" as the path separator by default
$winPath = "c:/appserv/www/index.php"; – also accept "/" as the path separator in the Windows system, recommended for use
$fileName 1= "file.txt"; – Relative path, file.txt file in current directory
$fileName 2= "Javascript/common.js"; – Relative path, the Common.js file in the JavaScript subdirectory of the current directory
$fileName 3 = ". /images/logo.gif "; – Relative path, logo.gif file under Images subdirectory in the previous level directory

In the example above, the format of absolute and relative paths in UNIX and Windows systems is listed separately. The UNIX system must use a forward slash "/" as the path separator, and the default backslash "\" is used as the path separator in the Windows system, and "\" in the program, but also the forward slash "/" as the delimiter. It is recommended that you use "/" as the path separator for a file in order for the program to be well ported. Alternatively, you can use PHP's built-in constant Directory_separator, whose value is the default file path separator for the current operating system. For example:

Copy Code code as follows:

$fileName 2 = "JavaScript". Directory_separator. " Common.js "; –unix is "/", Windows is "\"

It is often useful to separate the attributes from the directory path, such as the extension at the end, the directory section, and the base name. These tasks can be accomplished by using PHP's system Functions basename (), DirName (), and PathInfo () functions.

① function basename ()

function basename () returns the file name portion of the path. The prototype of the function looks like this:

Copy Code code as follows:

String basename (string path[,string suffix])//return filename part of Path

This function gives a string containing its full path to a file, which returns the basic file name. The second parameter is an optional parameter that sets the file's extension. If provided, this extension is not exported. The function is used as shown in the following code:
Copy Code code as follows:

<?php
Contains a string that has all the paths to a file
$path = "/var/www/html/page.php";
Display filename with file name extension, output page.php
Echo basename ($path);
Displays a filename with no file name extension, output page
Echo basename ($path, ". php");
?>

② function DirName ()

This function is exactly the opposite of basename () and requires only one argument, a string containing all the paths to a file, which returns the name of the directory after the file name is removed. The function is used as shown in the following code:

Copy Code code as follows:

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

③ function PathInfo ()

function PathInfo () returns an associative array that includes the directory name, base name, and extension three portions of the specified path. are referenced by array keys dirname, basename, and extension respectively. The function is shown using the following code.

Copy Code code as follows:

<?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
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.