Home  >  Article  >  Backend Development  >  php dirname、basename、pathinfo 解析目录路径

php dirname、basename、pathinfo 解析目录路径

PHP中文网
PHP中文网Original
2017-06-05 10:00:451168browse

本文章详细的介绍了关于php dirname、basename、pathinfo 解析目录路径,有需要学习的朋友可参考一下。

1:string basename(string path[,string suffix]);

给出一个包含有指向一个文件的全路径的字符串,本函数返回基本的文件名。如果文件名是以suffix结束的,那这一部分也会被去掉。

在Windows中,斜线(/)和反斜线()都可以用作路径分隔符。在其它环境下是斜线(/)。

例子1.basename()例子,代码如下

参数是是一个文件路径的字符串,返回去掉文件名后的目录
2:string dirname(string path);

__FILE__的路径是当前代码所在文件
 
dirname(dirname(__FILE__));得到的是文件上一层目录名
dirname(__FILE__);得到的是文件所在层目录名

例子,代码如下

输出:

c:/testweb
/testweb

参数是一个文件路径的字符串,返回一个包含目录名、文件名和扩展名三个部分的数组,分别通过dirname、basename、extension来引用3:array pathinfo($path);

例子 1

输出结果:

Array
(
[dirname] => /testweb
[basename] => test.txt
[extension] => txt
)

例子 2

输出结果:

test.txt
Statement:
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