且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 PHP 中检查上传表单中的文件扩展名

更新时间:2022-11-27 15:51:25

使用 if( $ext !== 'gif') 可能效率不高.如果您允许 20 个不同的扩展名怎么办?

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

试试:

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