php pathinfo()函數

一.pathinfo()函數 php

pathinfo()函數返回一個包含了文件信息的數組,數組中有四個元素,分別是dirname、basename、extension、filename。數組

舉例:函數

<?phpio

//pathinfo函數用法function

$path = "/www/jbxue/images/logo.jpg";file

$fileArr = pathinfo($path);方法

print_r($fileArr);im

//輸出結果:Array ( [dirname] => /www/jbxue/images [basename] => logo.jpg [extension] => jpg [filename] => logo )di

//根據數組的鍵名就能夠得到對應的鍵值文件

echo $fileArr['filename'];

//輸出結果:logo

echo $fileArr['extension'];

//輸出結果:jpg

?>

二.另外一種實現方法

function path_info($filepath)  

{  

    $path_parts = array();  

    $path_parts ['dirname'] = rtrim(substr($filepath, 0, strrpos($filepath, '/')),"/")."/";  

    $path_parts ['basename'] = ltrim(substr($filepath, strrpos($filepath, '/')),"/");  

    $path_parts ['extension'] = substr(strrchr($filepath, '.'), 1);  

    $path_parts ['filename'] = ltrim(substr($path_parts ['basename'], 0, strrpos($path_parts ['basename'], '.')),"/");  

    return $path_parts;

$path=path_info("/www/jbxue/images/logo.jpg");

var_dump($path);

相關文章
相關標籤/搜索