search
Homephp教程PHP源码php获取文件名称和扩展名的方法_php实例

本篇文章主要介绍了php获取文件名称和扩展名的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

php获取文件名称和扩展名

1.basename()-返回路径的文件名

请看下面php代码:

<?php
$path =
"/usr/www/html/index.php";
echo basename($path)."<br>";
//如果选择suffix则忽略扩展名
echo basename($path,".php");
?>

运行结果:

index.php

index

2.dirname()-返回当前脚本的文件路径!

php代码:

<?php
//——FILE__返回文件完整路径
$dir = dirname(__FILE__);
echo $dir;
?>

运行结果:

F:\web\zend\exercise

3.pathinfo() 返回一个关联数组包含有 path 的信息。

包括以下的数组单元:路径名称dirname,文件名称basename 和 扩展名称extension。

请看下面简单代码演示:

<?php
$path =
"/usr/www/html/index.php";
$pathinfo 
= pathinfo($path);
echo "目录名称:$pathinfo[dirname]<br>";
echo "文件名称:$pathinfo[basename]<br>";
echo "扩展名:$pathinfo[extension]";
?>

运行结果:

目录名称:/usr/www/html

文件名称:index.php

扩展名:php

4.realpath -- 返回规范化的绝对路径名

php代码如下:

<?php
$path =
"./exercise/php.txt";
$realpath 
= realpath($path);
echo $realpath;
?>

最后注意一个小技巧:不同路径的文件路径操作符可能,不同,windows下可以用"/"和"\",

linux下只能用"/",所以开发的时候,建议都用"/",如我上面的文件路径写法!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持php中文网。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.