php根据路径获取文件名

<?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 />";

}

posted @ 2016-11-28 16:37  _即炤  阅读(7862)  评论(0编辑  收藏  举报