• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 4年前 (2022-01-01) 27次浏览 已收录 0个评论

有时候我们后台需要设置用户只能上传指定后缀名的文件,那么就来源gao*daima.com搞@代#码网可以使用下面的代码了

核心代码

 /** * 获取文件后缀名,并判断是否合法 * * @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。所以gaodaima搞代码网小编换了如下方法

 <?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-600",$allow)){ echo "ok"; }else{ echo "no"; }

如此就解决了,希望大家以后多多支持gaodaima搞代码网

以上就是php 获取文件后缀名,并判断是否合法的函数的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php 获取文件后缀名,并判断是否合法的函数
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址