php根据路径获取文件名

时间:2022-03-16 09:28:35

<?php

// 根据路径返回文件名

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

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

$path = $path = str_replace('\\', '/', $path);

if (substr($path, -1) != '/') {

$path = $path . '/';

}

echo $path;

br();

// 方法一, substr

$path = trim($path, '/');

echo substr($path, strripos($path, '/') + 1);

br();

// 方法二, basename

echo basename($path);

br();

// 方法三, dirname

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

br();

// 方法四, pathinfo

$pathinfo = pathinfo($path);

echo $pathinfo['basename'];

br();

// 方法五, explode

$patharr = explode('/',$path);

echo array_pop($patharr);

br();

function br() {

echo "<br />";

}