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】。

相关文章

PHP排序算法类实例

本文实例讲述了PHP排序算法类。分享给大家供大家参考。具体如下: 四种排序算法的PHP实现: 1) 插入排序(Insertion Sort)的基本思想是: 每次将一个待排序的记录,按其...

php下实现折线图效果的代码

<?php   Class ImageReport{  var $X;//图片大小X轴  var $Y;//图...

实现dedecms全站URL静态化改造的代码

转自bbs.dedecms.com 1、将include复制到网站中的include目录。 2、修改数据库 将所有文档设置为“仅动态”,可以进入数据库管理中,执行下面命令: update...

php ci 获取表单中多个同名input元素值的代码

有时前台页面要允许动态增加/删除某项属性的多个值,比如向书架中添加书本,要可以动态增加或者删除书本。前台页面的表单中会有多个input元素,如下: 复制代码 代码如下: <form...

php serialize()与unserialize() 不完全研究

serialize()和unserialize()在php手册上的解释是: serialize — Generates a storable representation of a va...