首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在PHP中限制文件上传型文件大小?

如何在PHP中限制文件上传型文件大小?
EN

Stack Overflow用户
提问于 2012-02-06 05:32:45
回答 5查看 136.5K关注 0票数 25

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

有四种场景:

不正确的大小/正确的类型(working)

  • Correct大小/不正确的类型(working)

  • INCORRECT大小/正确的类型(不是working)

  • INCORRECT大小/不正确的类型(not working)

(

  1. not

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

你知道为什么吗?

代码语言:javascript
复制
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);
    }

}   
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-02-06 05:56:02

您的代码没有考虑到的是显示多个错误。如上所述,用户可以上传类型错误的文件,但您的代码只能报告其中一个问题。尝试如下所示:

代码语言:javascript
复制
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
    }
}

查看move_uploaded_file()的文档(称为move而不是store)以了解更多信息。

票数 47
EN

Stack Overflow用户

发布于 2014-01-02 15:45:32

希望这能有所帮助:-)

代码语言:javascript
复制
if(isset($_POST['submit'])){
    ini_set("post_max_size", "30M");
    ini_set("upload_max_filesize", "30M");
    ini_set("memory_limit", "20000M"); 
    $fileName='product_demo.png';

    if($_FILES['imgproduct']['size'] > 0 && 
            (($_FILES["imgproduct"]["type"] == "image/gif") || 
                ($_FILES["imgproduct"]["type"] == "image/jpeg")|| 
                ($_FILES["imgproduct"]["type"] == "image/pjpeg") || 
                ($_FILES["imgproduct"]["type"] == "image/png") &&
                ($_FILES["imgproduct"]["size"] < 2097152))){

        if ($_FILES["imgproduct"]["error"] > 0){
            echo "Return Code: " . $_FILES["imgproduct"]["error"] . "<br />";
        } else {    
            $rnd=rand(100,999);
            $rnd=$rnd."_";
            $fileName = $rnd.trim($_FILES['imgproduct']['name']);
            $tmpName  = $_FILES['imgproduct']['tmp_name'];
            $fileSize = $_FILES['imgproduct']['size'];
            $fileType = $_FILES['imgproduct']['type'];  
            $target = "upload/";
            echo $target = $target .$rnd. basename( $_FILES['imgproduct']['name']) ; 
            move_uploaded_file($_FILES['imgproduct']['tmp_name'], $target);
        }
    } else {
        echo "Sorry, there was a problem uploading your file.";
    }
}
票数 4
EN

Stack Overflow用户

发布于 2012-02-06 06:25:27

如果您正在寻找站点上所有上传的硬限制,您可以通过以下设置在php.ini中对其进行限制:

upload\_max\_filesize = 2M post\_max\_size = 2M

这会将最大上传限制设置为2 MB

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9153224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档