Home >Backend Development >PHP Tutorial >4 ways to get file extension in php
This article mainly introduces 4 methods for PHP to obtain file extensions, involving PHP's related operating techniques for strings, arrays and uploaded file attributes. It has certain reference value. Friends in need You can refer to the following examples
This article describes 4 methods for obtaining file extensions in PHP. Share it with everyone for your reference, the details are as follows:
$filename="123.jpg"; //方法一: function get_ext($file_name){ return array_pop(explode('.', $file_name)); //用.号对字符串进行分组 } echo get_ext($filename); //方法二: $fileEx=strtolower(substr(strrchr($filename,"."),1)); echo $fileEx; //方法三: $extend=pathinfo($filename); echo $extend['extension']; //方法四: $filetype=array("image/gif","image/jpeg"); //判断文件扩展名类型是否在该 数组中 if(in_array($_FILES['file']['type'],$filetype)){ //针对上传文件判断 echo $_FILES['file']['type']; }
The above is the detailed content of 4 ways to get file extension in php. For more information, please follow other related articles on the PHP Chinese website!