
原标题:php 如何获取文件后缀
PHP 获取文件后缀的几种方法
1、通过 explode 分割数组,后去最后一个元素文章来源地址https://www.yii666.com/learning/php/15.html
<?php $filename = 'https://www.yii666.com/style/defalut/img/logo-bold.png'; $array = explode('.',$filename); $ext = end($array); ?>
2、通过 pathinfo 函数,函数以数组的形式返回文件路径的信息。
<?php $filename = 'https://www.yii666.com/style/defalut/img/logo-bold.png'; $file = pathinfo($filename); $ext = $file['extension']; ?>
3、使用 getimagesize 函数,函数返回图像的尺寸以及文件类型(可用于 base64 编码图片)文章来源地址https://www.yii666.com/learning/php/15.htmlwww.yii666.com
<?php $filename = 'https://www.yii666.com/style/defalut/img/logo-bold.png'; $info = getimagesize($filename); $data = explode('/',$info['mime']); $ext = end($data); ?>
4、用 strrpos 函数获取 "." 最后出现的位置,通过 substr 函数返回位置后的字符串
<?php $filename = 'https://www.yii666.com/style/defalut/img/logo-bold.png'; $ext1 = substr($filename,strrpos($filename,'.')); // 带 "." $ext2 = substr($filename,strrpos($filename,'.')+1);// 不带 "." ?>
5、通过正则方式获取文件后缀,不带“.”请去掉正则里面的 “\.”
<?php $filename = 'https://www.yii666.com/style/defalut/img/logo-bold.png'; preg_match("/(\.gif|\.jpg|\.png|\.jpeg)$/",$filename,$match); $ext = $match[0]; ?>
来源于:php 如何获取文件后缀