PHP function introduction—dirname(): returns the directory part of the path

PHPz
Release: 2023-07-24 18:52:01
Original
1697 people have browsed it

PHP function introduction—dirname(): Returns the directory part of the path

In PHP development, it is often necessary to operate the file path, and obtaining the directory part of the file is a common requirement. PHP provides the dirname() function to return the directory portion of a given path.

The syntax of the dirname() function is as follows:
string dirname (string $path [, int $levels = 1])

Function parameters:

  • path: required, to get the file path of the directory part.
  • levels: Optional, specify the number of upper-level directories in the return path. Default is 1.

The following is a simple example using the dirname() function:

$path = '/var/www/html/index.php';
$dir = dirname($path);

echo $dir; // 输出:/var/www/html
Copy after login

In the above example, we used a file path "/var/www/html/index.php ", then call the dirname() function to get the directory part of the path, and assign the result to the variable $dir. Finally, we use the echo statement to output $dir, and the result is "/var/www/html".

As you can see, the dirname() function successfully returns the directory part of the file path. This function will automatically recognize the path separators of different operating systems and can work normally on different operating systems.

In addition, the dirname() function also has an optional parameter levels, which is used to specify the number of superior directories in the return path. By default, levels is 1, which means the one-level superior directory in the return path is returned. If the needs are different, this value can be adjusted according to the actual situation.

The following is an example demonstrating the levels parameter:

$path = '/var/www/html/index.php';
$dir = dirname($path, 2);

echo $dir; // 输出:/var/www
Copy after login

In the above example, we passed a levels value of 2, telling PHP to return the two-level parent directory in the path. The result shows "/var/www", which is the superior directory of the superior directory.

Summary:

  • The dirname() function is a function in PHP used to obtain the directory part of the file path.
  • You can specify the number of upper-level directories in the return path by setting the levels parameter.
  • The dirname() function is suitable for the path formats of different operating systems.
  • Using the dirname() function can simplify the processing and operation of file paths.

In PHP development, the dirname() function is a very useful function that can help us easily obtain the directory part of the file path. Whether you are dealing with file uploads or file operations, understanding and skillfully using the dirname() function can improve the readability and maintainability of your code.

The above is the detailed content of PHP function introduction—dirname(): returns the directory part of the path. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template