php在上传表单中检查文件扩展名 [英] php check file extension in upload form

查看:107
本文介绍了php在上传表单中检查文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检查文件扩展名是否上传。我的方法工作,但现在我需要明白,我的方法(pathinfo)是真实的?另一种更好更快的方式?谢谢

  $ filename = $ _FILES ['video_file'] ['name']; 
$ ext = pathinfo($ filename,PATHINFO_EXTENSION);
if($ ext!=='gif'|| $ ext!=='png'|| $ ext!=='jpg'){echo'error';}
if($ ext!=='gif')





















$ b $ p> $ allowed = array('gif','png','jpg');
$ filename = $ _FILES ['video_file'] ['name'];
$ ext = pathinfo($ filename,PATHINFO_EXTENSION);
if(!in_array($ ext,$ allowed)){
echo'error';
}


I check file extension for upload or not uploaded. my i.e methods worked but now i need to understand, my methods (pathinfo) is true ? another better and faster way ?! Thanks

$filename = $_FILES['video_file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if( $ext !== 'gif' || $ext !== 'png' || $ext !== 'jpg' ) {echo 'error';}

解决方案

Using if( $ext !== 'gif') might not be efficient what if you allow like 20 different extensions

Try

$allowed =  array('gif','png' ,'jpg');
$filename = $_FILES['video_file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) ) {
    echo 'error';
}

这篇关于php在上传表单中检查文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆