Home > Backend Development > PHP Tutorial > Detailed explanation of the php pathinfo() function to obtain the path, name and other information of the file

Detailed explanation of the php pathinfo() function to obtain the path, name and other information of the file

怪我咯
Release: 2023-03-07 21:20:02
Original
3947 people have browsed it

In PHP, it is very easy to obtain the path, name, or extension of a file through functions. You can use the php pathinfo() function we talked about earlier or you can use the php dirname() function, php basename() function and other ways to obtain the corresponding information.

PHP pathinfo() function

Definition and usage

pathinfo() function returns the file path in the form of an array or string Information.
The returned array elements are as follows:
[dirname]: Returns the directory part in the file path
[basename]: Returns the part of the file name in the file path
[extension]: Returns the file in the file path Part of the type
Syntax

pathinfo(path,options)
Copy after login
ParametersDescription
pathRequired. Specifies the path to be checked.
optionsOptional. Specifies the array elements to be returned. The default is all.

Possible values:

  • PATHINFO_DIRNAME - only returns dirname

  • PATHINFO_BASENAME - only returns basename

  • PATHINFO_EXTENSION - Returns only extension

##Tips and comments

Comments: If Instead of requesting all elements, the pathinfo() function returns a string.

php turns on pathinfo routing mode: pathinfo mode requires php.ini to turn on the following parameter

cgi.fix_pathinfo=1
Copy after login

path_info mode: h t t p://www.xxx.com/index.php/ module/method


Example 1

 <?php
 print_r(pathinfo("/testweb/test.txt"));
 ?>
Copy after login


The above code will output:

Detailed explanation of the php pathinfo() function to obtain the path, name and other information of the file

Example 2

<?php
 var_dump(pathinfo("/testweb/test.txt",PATHINFO_DIRNAME));
 var_dump(pathinfo("/testweb/test.txt",PATHINFO_BASENAME));
 var_dump(pathinfo("/testweb/test.txt",PATHINFO_EXTENSION));
 ?>
Copy after login

The above code will output:


Detailed explanation of the php pathinfo() function to obtain the path, name and other information of the file

Assume that there is an image file now, and its server-side path is:


$path = "/www/mywebsite/images/myphoto.jpg";

1.pathinfo() function
pathinfo() function returns a file containing file information Array, there are four elements in the array, namely dirname, basename, extension, filename. The code to print the array:

The code is as follows:

<?php
$path = "/www/mywebsite/images/myphoto.jpg";
$fileArr = pathinfo($path);
print_r($fileArr);
?>
Copy after login

The code running result:

Detailed explanation of the php pathinfo() function to obtain the path, name and other information of the file

In this way, we only need to print the array according to the The corresponding key value can be obtained by key name:


The code is as follows:

<?php
$path = "/www/mywebsite/images/myphoto.jpg";
$fileArr = pathinfo($path);
echo $fileArr[&#39;filename&#39;]."<br/>";
//输出结果:myphoto
echo $fileArr[&#39;extension&#39;];
//输出结果:jpg
?>
Copy after login

The code running result:

Detailed explanation of the php pathinfo() function to obtain the path, name and other information of the file

2.dirname() function dirname() function gives a string containing the full path to a file. The value it returns is the directory name after removing the file name, which can be considered Extension of the pathinfo() function:

The code is as follows:

<?php
$path = "/www/mywebsite/images/myphoto.jpg";
$fileArr = pathinfo($path);
echo dirname($path)."<br/>";
//输出结果:/www/mywebsite/images
//或者
echo dirname("/www/mywebsite/images/")."<br/>";
echo dirname("/www/mywebsite/images");
//输出的结果都为:/www/mywebsite
?>
Copy after login

The code running result:

Detailed explanation of the php pathinfo() function to obtain the path, name and other information of the file

So it can be understood as The returned value is the directory address name of the upper layer of the path.


3.basename() function

The basename() function gives a full name that points to a file. The path string, the value returned is the basic file name, which can also be considered as an extension of the pathinfo() function:

The code is as follows:

<?php
$path = "/www/mywebsite/images/myphoto.jpg";
$fileArr = pathinfo($path);
echo basename($path)."<br/>";
//输出结果:myphoto.jpg
//或者
echo basename("/www/mywebsite/images/");
//输出结果:images
?>
Copy after login
Code running result:

Detailed explanation of the php pathinfo() function to obtain the path, name and other information of the file

So it can be understood that the returned value is the name of the current directory of the path.

【Recommended related articles】:

1. Detailed explanation of the php pathinfo() function to obtain file path information

2. Detailed explanation of the usage of php dirname() function to obtain file information

3. Detailed explanation of php Usage of basename() function to obtain file name

The above is the detailed content of Detailed explanation of the php pathinfo() function to obtain the path, name and other information of the file. 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