PHP function introduction—pathinfo(): returns file information in the path

WBOY
Release: 2023-07-25 08:34:02
Original
2034 people have browsed it

PHP function introduction—pathinfo(): Returns file information in the path

In the process of web development, we often need to operate files, such as obtaining file information, file paths, etc. PHP provides many functions to handle these needs. Among them, the pathinfo() function is very useful, it can easily extract file-related information from a file path.

The basic syntax of the pathinfo() function is as follows:

pathinfo($path, $options);
Copy after login

$path is the file path, $options are optional parameters, used to control the output results. This function returns an array containing various information in the file path. Next, let’s look at a few commonly used options.

  1. PATHINFO_DIRNAME: Get the directory part of the path
  2. PATHINFO_BASENAME: Get the file name part of the path
  3. PATHINFO_EXTENSION: Get the file extension part of the path
  4. PATHINFO_FILENAME: Get the file name (excluding extension) part of the path

The following are some code examples to show the use of the pathinfo() function:

// 获取路径的目录部分
$path = "/home/user/www/example.php";
$dirname = pathinfo($path, PATHINFO_DIRNAME);
echo "目录:".$dirname."
";

// 获取路径的文件名部分
$basename = pathinfo($path, PATHINFO_BASENAME);
echo "文件名:".$basename."
";

// 获取路径的文件扩展名部分
$extension = pathinfo($path, PATHINFO_EXTENSION);
echo "扩展名:".$extension."
";

// 获取文件名(不包含扩展名)部分
$filename = pathinfo($path, PATHINFO_FILENAME);
echo "文件名(不包含扩展名):".$filename."
";
Copy after login

Running the above code, you will get the following results:

目录:/home/user/www
文件名:example.php
扩展名:php
文件名(不包含扩展名):example
Copy after login

Through the pathinfo() function, we can easily get each part of the path. This is often used in development, especially in file uploading and processing. We can easily obtain the directory, file name, extension and other information of the file for further operations.

Summary:

  • The pathinfo() function is a very convenient function in PHP. It can easily extract various information about the file from the file path.
  • PATHINFO_DIRNAME is used to get the directory part of the path.
  • PATHINFO_BASENAME is used to get the file name part of the path.
  • PATHINFO_EXTENSION is used to get the file extension part of the path.
  • PATHINFO_FILENAME is used to get the file name (excluding extension) part of the path.

I hope this article can help everyone better understand and use the pathinfo() function and bring convenience to development work.

The above is the detailed content of PHP function introduction—pathinfo(): returns file information in 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!