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)
Parameters | Description |
---|---|
path | Required. Specifies the path to be checked. |
options | Optional. Specifies the array elements to be returned. The default is all. Possible values:
|
##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
Example 1
<?php print_r(pathinfo("/testweb/test.txt")); ?>
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)); ?>
$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:
<?php $path = "/www/mywebsite/images/myphoto.jpg"; $fileArr = pathinfo($path); print_r($fileArr); ?>
The code is as follows:
<?php $path = "/www/mywebsite/images/myphoto.jpg"; $fileArr = pathinfo($path); echo $fileArr['filename']."<br/>"; //输出结果:myphoto echo $fileArr['extension']; //输出结果:jpg ?>
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:
<?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 ?>
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:
<?php $path = "/www/mywebsite/images/myphoto.jpg"; $fileArr = pathinfo($path); echo basename($path)."<br/>"; //输出结果:myphoto.jpg //或者 echo basename("/www/mywebsite/images/"); //输出结果:images ?>
【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!