Home >Backend Development >PHP Tutorial >How to get file name by path in php (code)

How to get file name by path in php (code)

不言
不言Original
2018-08-22 15:19:404553browse

本篇文章给大家带来的内容是关于php如何通过路径获取文件名(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

<?php

// 根据路径返回文件名

$path = &#39;J:\abc\defg\hijk\一个文件夹\lmn\opq&#39;;

$path = iconv("UTF-8", "GB2312//IGNORE", $path);

$path = $path = str_replace(&#39;\\&#39;, &#39;/&#39;, $path);

if (substr($path, -1) != &#39;/&#39;) {

$path = $path . &#39;/&#39;;

}

echo $path;

br();

// 方法一, substr

$path = trim($path, &#39;/&#39;);

echo substr($path, strripos($path, &#39;/&#39;) + 1);

br();

// 方法二, basename

echo basename($path);

br();

// 方法三, dirname

echo substr($path, strlen(dirname($path)) + 1);

br();

// 方法四, pathinfo

$pathinfo = pathinfo($path);

echo $pathinfo[&#39;basename&#39;];

br();

// 方法五, explode

$patharr = explode(&#39;/&#39;,$path);

echo array_pop($patharr);

br();

 

function br() {

echo "<br />";

}

相关推荐:

如何使用PHP获取文件相对路径

PHP获取当前文件路径,下层目录路径

The above is the detailed content of How to get file name by path in php (code). For more information, please follow other related articles on the PHP Chinese website!

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

Related articles

See more