PHP pathinfo()获得文件的路径、名称等信息说明
假设现在有一个图片文件,它的服务器端路径为:
$path = "/www/mywebsite/images/myphoto.jpg";
1.pathinfo()函数
pathinfo()函数返回的是一个包含了文件信息的数组,数组中有四个元素,分别是dirname、basename、extension、filename。打印数组的代码:
复制代码 代码如下:
$fileArr = pathinfo($path);
print_r($fileArr);
//输出结果:Array ( [dirname] => /www/mywebsite/images [basename] => myphoto.jpg [extension] => jpg [filename] => myphoto )
这样我们只需根据数组的键名就可以获得对应的键值:
复制代码 代码如下:
echo $fileArr['filename'];
//输出结果:myphoto
echo $fileArr['extension'];
//输出结果:jpg
//...
2.dirname()函数
dirname()函数给出一个包含有指向一个文件的全路径的字符串,它返回的值去掉文件名后的目录名,可以认为是对pathinfo()函数的扩展:
复制代码 代码如下:
echo dirname($path);
//输出结果:/www/mywebsite/images
//或者
echo dirname("/www/mywebsite/images/");
echo dirname("/www/mywebsite/images");
//输出的结果都为:/www/mywebsite
所以可以理解为返回的值为路径的上一层目录地址名。
3.basename()函数
basename()函数给出一个包含有指向一个文件的全路径的字符串,他返回的值为基本的文件名,同样可以认为是对pathinfo()函数的扩展:
复制代码 代码如下:
echo basename($path);
//输出结果:myphoto.jpg
//或者
basename("/www/mywebsite/images/");
//输出结果:images
所以可以理解为返回的值为路径的当前目录的名称。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)