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

yipeiwu_com6年前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】。

相关文章

PHP折半(二分)查找算法实例分析

本文实例讲述了PHP折半(二分)查找算法。分享给大家供大家参考,具体如下: 折半查询只适用于已经按照正序或者逆序排序的数组,字符串等; 算法: 先取数组的中间位置,无中间位置,则向下取整...

浅析php header 跳转

PHP的header函数 可以很少代码就能实现HTML代码中META 标签这里只说用 header函数来做页面的跳转 1. HTML代码中页面的跳转的代码HTML meta refres...

THINKPHP在添加数据的时候获取主键id的值方法

在使用ThinkPHP新增数据后可以很方便的获取自动增长型的主键值。 $Model = D(‘Blog'); $data['name'] = 'test'; $data['tit...

Window下PHP三种运行方式图文详解

Window下PHP三种运行方式图文详解

PHP能不能成功的在Apache服务器上运行,就看我们如何去配置PHP的运行方式。PHP运行目前为止主要有三种方式: a、以模块加载的方式运行,初学者可能不容易理解,其实就是将PHP集成...

php实现TCP端口检测的方法

本文实例讲述了php实现TCP端口检测的方法。分享给大家供大家参考。具体如下: 该程序可以确认当前端口是否可用: <?php class Health { publ...