php實(shí)現(xiàn)文件上傳以及多文件上傳是比較重要且必需要掌握的一部分內(nèi)容,關(guān)于文件上傳的方法有很多,這里主要介紹其中的幾種方法,以供大家參考。
1、字符串查找分割方法
1.$file = 'x.y.z.png'; echo substr(strrchr($file, '.'), 1);
解析:strrchr($file, ‘.’)
strrchr() 函數(shù)查找字符串在另一個(gè)字符串中最后一次出現(xiàn)的位置,并返回從該位置到字符串結(jié)尾的所有字符
2.$file = 'x.y.z.png'; echo substr($file, strrpos($file, '.')+1);
解析:strrpos($file, ‘.’)
查找 “.” 在字符串中最后一次出現(xiàn)的位置,返回位置 substr()從該位置開始截取
2、數(shù)組分割方法
3.$file = 'x.y.z.png'; $arr=explode('.', $file); echo $arr[count($arr)-1]; 4.$file = 'x.y.z.png'; $arr=explode('.', $file); echo end($arr); 5.$file = 'x.y.z.png'; echo strrev(explode('.', strrev($file))[0]);
解析:end()返回?cái)?shù)組的最后一個(gè)元素,strrev(string) 函數(shù)反轉(zhuǎn)字符串。
3、路徑函數(shù)方法
6.$file = 'x.y.z.png'; echo pathinfo($file)['extension']; //參數(shù)如下 [dirname] [basename] [extension] 7.$file = 'x.y.z.png'; echo pathinfo($file, PATHINFO_EXTENSION);
解析:pathinfo() 函數(shù)以數(shù)組的形式返回文件路徑的信息。
以上總共介紹了7種獲取文件后綴名的方法,僅供大家參考,想了解