如何在PHP中限制文件上传类型的文件大小? [英] How to limit file upload type file size in PHP?

查看:164
本文介绍了如何在PHP中限制文件上传类型的文件大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个上传表单,我正在检查文件大小和文件类型,将上传的文件限制为2 MB,.pdf,.jpg,.gif或.png文件类型。我的目标是向用户显示警告消息,如果他们违反了这些规则之一。



有四种情况:


  1. 正确大小/正确类型(工作)

  2. 正确大小/不​​正确类型(工作)

  3. 不正确大小/正确类型(不工作

  4. 不正确大小/不​​正确类型(不工作

使用我当前的代码,当文件大小大于2兆字节(#4)时,它总是显示不正确的类型消息,即使文件类型为正确(#3)。



任何想法为什么?

  if(isset($ _FILES [ 'uploaded_file'])){

$ file_size = $ _FILES ['uploaded_file'] ['size'];
$ file_type = $ _FILES ['uploaded_file'] ['type'];

如果(($ file_size> 2097152)){
$ message ='文件太大。文件必须小于2兆字节。
echo'< script type =text / javascript> alert('。$ message。');< / script>';
}
elseif(
($ file_type!=application / pdf)&
($ file_type!=image / jpeg)&&
($ file_type!=image / jpg)&
($ file_type!=image / gif)&
($ file_type!=image / png )
){
$ message ='无效的文件类型。只接受PDF,JPG,GIF和PNG类型。
echo'< script type =text / javascript> alert('。$ message。');< / script>';
}
else {
store_uploaded_file($ id);
}

}


解决方案

您的代码无法解释的是显示多个错误。如上所述,用户可以上传2MB的错误类型的文件,但代码只能报告其中一个问题。尝试一下:

  if(isset($ _ FILES ['uploaded_file'])){
$ errors = array ();
$ maxsize = 2097152;
$ acceptable = array(
'application / pdf',
'image / jpeg',
'image / jpg',
'image / gif',
'image / png'
); $($ _FILES [upload_file] [size] == 0)){($ _ FILES ['upload_file'] ['size']> = $ maxsize)
$ errors [] ='文件太大。文件必须小于2兆字节。
}

if(!in_array($ _ FILES ['uploaded_file'] ['type'],$ acceptable))&& (!empty($ _ FILES [uploaded_file] [type]))){
$ errors [] ='无效的文件类型。只接受PDF,JPG,GIF和PNG类型。
}

if(count($ errors)=== 0){
move_uploaded_file($ _ FILES ['uploaded_file'] ['tmpname'],'/ store / to /location.file');
} else {
foreach($ errors as $ error){
echo'< script> alert('。$ error。');< / script>';
}

die(); //确保没有更多的处理完成
}
}

move_uploaded_file() (被称为移动不存储)更多。


I have an upload form and am checking the file size and file type to limit the uploaded file to 2 megabytes and either .pdf, .jpg, .gif or .png file types. My goal is to have an alert message displayed to the user if they violate one of these rules.

There are four scenarios:

  1. Correct Size / Correct Type (working)
  2. Correct Size / INCORRECT Type (working)
  3. INCORRECT Size / Correct Type (not working)
  4. INCORRECT Size / INCORRECT Type (not working)

With my current code, it always displays the incorrect "type" message when the file size is greater than 2 megabytes (#4), even if the file type is correct (#3).

Any ideas why?

if (isset ( $_FILES['uploaded_file'] ) ) {

    $file_size = $_FILES['uploaded_file']['size'];
    $file_type = $_FILES['uploaded_file']['type'];

    if (($file_size > 2097152)){      
        $message = 'File too large. File must be less than 2 megabytes.'; 
        echo '<script type="text/javascript">alert("'.$message.'");</script>'; 
    }
    elseif (  
        ($file_type != "application/pdf") &&
        ($file_type != "image/jpeg") &&
        ($file_type != "image/jpg") &&
        ($file_type != "image/gif") &&
        ($file_type != "image/png")    
    ){
        $message = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.'; 
        echo '<script type="text/javascript">alert("'.$message.'");</script>';         
    }    
    else {
        store_uploaded_file($id);
    }

}   

解决方案

Something that your code doesn't account for is displaying multiple errors. As you have noted above it is possible for the user to upload a file >2MB of the wrong type, but your code can only report one of the issues. Try something like:

if(isset($_FILES['uploaded_file'])) {
    $errors     = array();
    $maxsize    = 2097152;
    $acceptable = array(
        'application/pdf',
        'image/jpeg',
        'image/jpg',
        'image/gif',
        'image/png'
    );

    if(($_FILES['uploaded_file']['size'] >= $maxsize) || ($_FILES["uploaded_file"]["size"] == 0)) {
        $errors[] = 'File too large. File must be less than 2 megabytes.';
    }

    if(!in_array($_FILES['uploaded_file']['type'], $acceptable)) && (!empty($_FILES["uploaded_file"]["type"]))) {
        $errors[] = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.';
    }

    if(count($errors) === 0) {
        move_uploaded_file($_FILES['uploaded_file']['tmpname'], '/store/to/location.file');
    } else {
        foreach($errors as $error) {
            echo '<script>alert("'.$error.'");</script>';
        }

        die(); //Ensure no more processing is done
    }
}

Look into the docs for move_uploaded_file() (it's called move not store) for more.

这篇关于如何在PHP中限制文件上传类型的文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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