php 获取文件后缀名,并判断是否合法的函数

yipeiwu_com5年前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。所以【宜配屋www.yipeiwu.com】小编换了如下方法

<?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";
}

如此就解决了,希望大家以后多多支持【宜配屋www.yipeiwu.com】。

相关文章

IStream与TStream之间的相互转换

   procedure TForm1.Button1Click(Sender: TObject);    ...

php实现多站点共用session实现单点登录的方法详解

php实现多站点共用session实现单点登录的方法详解

本文实例讲述了php实现多站点共用session实现单点登录的方法。分享给大家供大家参考,具体如下: 最近闲来无事,总结整理下单点登录的问题。 单点登录的基本原理为:客户端共享sesio...

简单谈谈php浮点数精确运算

bc是Binary Calculator的缩写。bc*函数的参数都是操作数加上一个可选的 [int scale],比如string bcadd(string $left_operand,...

用缓存实现静态页面的测试

<?php  function CreateShtml()  {  ob_start(array("callback_CreateShtml...

php 常用类汇总 推荐收藏

图表库下面的类库可以让你很简单就能创建复杂的图表和图片。当然,它们需要GD库的支持。pChart - 一个可以创建统计图的库。Libchart - 这也是一个简单的统计图库。JpGrap...