这篇文章将为大家详细讲解有关使用php怎么对文件后缀名进行获取,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
/**
* 获取文件后缀名,并判断是否合法
*
* @param string $file_name
* @param array $allow_type
* @return blob
*/
function get_file_suffix($file_name, $allow_type = array())
{
$file_suffix = strtolower(array_pop(explode('.', $file_name)));
if (empty($allow_type))
{
return $file_suffix;
}
else
{
if (in_array($file_suffix, $allow_type))
{
return true;
}
else
{
return false;
}
}
}
上面的对于php5.3以上的版本会报错Strict Standards: Only variables should be passed by reference in。所以亿速云小编换了如下方法
<?php
/**
* 获取文件后缀名,并判断是否合法
*
* @param string $file_name
* @param array $allow_type
* @return blob
*/
function get_file_suffix($file_name, $allow_type = array())
{
$fnarray=explode('.', $file_name);
$file_suffix = strtolower(array_pop($fnarray));
if (empty($allow_type))
{
return $file_suffix;
}
else
{
if (in_array($file_suffix, $allow_type))
{
return true;
}
else
{
return false;
}
}
}
$allow_wj="jpg,gif,png,jpeg";
$allow=explode(",",$allow_wj);
if (get_file_suffix("sakjdfk1.jpg",$allow)){
echo "ok";
}else{
echo "no";
}
关于使用php怎么对文件后缀名进行获取就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。