百科狗-知识改变命运!
--

pathinfo() - 返回文件路径的信息 - php 文件目录函数

是丫丫呀1年前 (2023-11-21)阅读数 20#技术干货
文章标签路径

pathinfo()

(PHP 4 >= 4.0.3, PHP 5, PHP 7)

返回文件路径的信息

说明

pathinfo(string $path[,int $options= PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME]): mixed

pathinfo()返回一个关联数组包含有path的信息。返回关联数组还是字符串取决于$options。

参数

$path

要解析的路径。

$options

如果指定了,将会返回指定元素;它们包括:PATHINFO_DIRNAMEPATHINFO_BASENAMEPATHINFO_EXTENSIONPATHINFO_FILENAME

如果没有指定$options默认是返回全部的单元。

返回值

pathinfo() - 返回文件路径的信息 - php 文件目录函数

如果没有传入$options,将会返回包括以下单元的数组array:dirnamebasenameextension(如果有),以及filename

Note:

If the$pathdoes not have an extension,noextensionelement will be returned(以下第二个案例)。

If$optionsis present, returns astringcontaining the requested element.

更新日志

版本说明
5.2.0添加了常量PATHINFO_FILENAME

范例

Example #1pathinfo()例子

以上例程会输出:

/www/htdocs/inc
lib.inc.php
php
lib.inc

Example #2pathinfo()example showing difference between null and no extension

以上例程的输出类似于:

string(0) ""
Notice: Undefined index: extension in test.php on line 6
NULL

注释

Note:

有关取得当前路径信息的说明,请阅读预定义变量一节。Note:

pathinfo()is locale aware, so for it to parse a path containing multibyte characters correctly, the matching locale must be set using thesetlocale()function.

参见

  • dirname() 返回路径中的目录部分
  • basename() 返回路径中的文件名部分
  • parse_url() 解析 URL,返回其组成部分
  • realpath() 返回规范化的绝对路径名
Simple example of pathinfo and array destructuring in PHP 7: 
Note:
pathinfo() is locale aware, so for it to parse a path containing multibyte characters correctly, the matching locale must be set using the setlocale() function. 
Reality:
var_dump(pathinfo('中国人2016.xls'));
exit();
array(4) { 'dirname' => string(1) "." 'basename' => string(8) "2016.xls" 'extension' => string(3) "xls" 'filename' => string(4) "2016" } 
Expect(Solve):
setlocale(LC_ALL, 'zh_CN.UTF-8');
var_dump(pathinfo('中国人2016.xls'));
exit();
array(4) { 'dirname' => string(1) "." 'basename' => string(17) "中国人2016.xls" 'extension' => string(3) "xls" 'filename' => string(13) "中国人2016" }
Use this function in place of pathinfo to make it work with UTF-8 encoded file names too 
Checked with version 5.5.12:
It works fine with filenames with utf-8 characters, pathinfo will strip them away:

.. will display:
Array
(
  [dirname] => /mnt/files
  [basename] => 飛兒樂團光茫.mp3
  [extension] => mp3
  [filename] => 飛兒樂團光茫
)
//pathinfo function example

//output
/*
Array
(
  [dirname] => /home/ramki
  [basename] => ramki.pdf
  [extension] => pdf
  [filename] => ramki
)
/home/ramki
ramki.pdf
pdf
ramki
*/
If a file has more than one 'file extension' (seperated by periods), the last one will be returned.
For example:

will produce:
Extension: gz
and not tar.gz
at example from "qutechie at gmail dot com" you can only replace function 'strpos' with 'strrpos'. (strrpos — Find position of last occurrence of a char in a string)
It's simple. For example:

Output will be:
Array
(
  [dirname] => /www/htdocs
  [basename] => index.html
  [extension] => html
  [filename] => index
)
Here is a simple function that gets the extension of a file. Simply using PATHINFO_EXTENSION will yield incorrect results if the path contains a query string with dots in the parameter names (for eg. &x.1=2&y.1=5), so this function eliminates the query string first and subsequently runs PATHINFO_EXTENSION on the clean path/url. 
Note that in PHP 4 (if you're stuck using it), pathinfo only provides dirname, basename, and extension, but not filename. This function will not split a file's stem and extension for you.
If you have filename with utf-8 characters, pathinfo will strip them away:
print_r(pathinfo("/mnt/files/飛兒樂團光茫.mp3"));
 .. will display:
Array
(
  [dirname] => /mnt/files
  [basename] => .mp3
  [extension] => mp3
  [filename] => 
)
about the path, there are one thing you should note : 
On Windows, both slash (/) and backslash (\) are used as directory separator character. In other environments, it is the forward slash (/). (this explain is from basename() function part https://www.php.net/manual/en/function.basename.php)
example: 
Here is an enhanced version of pathinfo() that interprets multi-part extensions like tar.gz as one file extension: 
A warning: this function varies depending on the platform it is being run on. For example, pathinfo('C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe') will return a different result when run through a winOS PHP platform (local development) vs. a server's UNIX-based OS. A bit like the Locale settings, but unexpected.
It's worth nothing that pathinfo returns foo/index.php for the directory when dealing with URLs like foo/index.php/bar
Note: dirname will be "." (meaning current directory) if the path is just a file name. (PHP 5.4.34) 
Sometimes, it's interessant to get the basename without extension.
So, I appended a new entry 'basenameWE' (Basename Without Extension) to the returned array.

Out :
Array
(
  [dirname] => /var/www/html
  [basename] => example.html
  [extension] => html
  [basenameWE] => example
)
This function is not perfect, but you can use it to convert a relative path to a URL.
Please email me if you can make any improvements. 
For a good example of how platform independent this function is have a look at the different return values that Lostindream and I experienced. Mine is above and Lostindream's is below:
Array
(
  [dirname] => /www/psychicblast/images/1
  [basename] => my three girlfriends.jpg
  [extension] => jpg
)
Array
(
  [dirname] => /www/htdocs
  [basename] => index.html
  [extension] => html
  [filename] => index
)
z
Note that pathinfo($somePath, PATHINFO_EXTENSION) will return '' (empty string) for both of these paths:
- some_random_file
- another_strange_file_ending_in_dot.
That's good, but then note that pathinfo($somePath, PATHINFO_FILENAME) won't end in the dot for 'another_strange_file_ending_in_dot.' - you'll need pathinfo($somePath, PATHINFO_BASENAME) to get the original filename ending in a dot.
Hope this helps!
PHP equivalent for custom implementations. Will be nearly as fast or faster (with long paths): 
Further to my previous post.
This affects servers that run PHP as a cgi module
If you have your own server:
You can use the AcceptPathInfo directive to force the core handler to accept requests with PATH_INFO and thereby restore the ability to use PATH_INFO in server-side includes.
Further information:
http://httpd.apache.org/docs-2.0/mod/core.html#acceptpathinfo
qutechie at gmail dot com wrote a fix for support for filename in PHP 4; however it gets it wrong whenever you have a filename with a . in it (so foo.bar.jpg would return foo instead of foo.bar).
A fix would be:

The idea is that you reverse the string and create a substring that starts after the first '.' and then reverse the result.
Quick fix for lack of support for 'filename' in php4 
Lightweight way to get extension for *nix systems

this will return NULL for dotfiles (hidden files)
Testing:

results:
string(3) "foo"
string(3) "bar"
string(0) ""
NULL
NULL
if you want to get all extensions (substring of file name after first dot) use another expression: 

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)