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)

不言
Release: 2023-04-03 21:18:01
Original
4528 people have browsed it

本篇文章给大家带来的内容是关于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 />";

}
Copy after login

相关推荐:

如何使用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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template