【问题标题】:File input problems - Limit number of selected files文件输入问题 - 限制选定文件的数量
【发布时间】:2011-07-02 10:29:30
【问题描述】:

我在这个“问题”中提出了两个问题...

1) 首先,我有一个<input type="file" name="file[]" multiple="multiple" />,现在我可以选择任意数量的图像,但我需要将这个数量限制为最多 10 个。我该怎么做?

2) 我的upload.php 文件如何处理多个上传的图像?除了上传单张图片外,我从来没有机会从事任何工作,但现在我被困在了这个问题上,我需要这个。我很困惑……

请帮忙?

【问题讨论】:

    标签: php html file-upload limit


    【解决方案1】:

    1 你可以使用javascript来检测选择的文件数量,如果超过10个会给出警告

    $('fileinput').onchange=function(){if(this.files.length>10)alert('to many files')}
    //prevent submitting if to many
    $('form').onsubmit=function(){if(this.files.length>10)return false;}
    

    您甚至可以通过将所有 .files[i].fileSize 相加来检查组合文件大小是否不大

    2 见:http://php.net/manual/en/features.file-upload.multiple.php (短版;使用:$_FILES['userfile']['name'][0], $_FILES['userfile']['tmp_name'][0], $_FILES['userfile']['size'] [0] 和 $_FILES['userfile']['type'][0])

    【讨论】:

    • fileSize 已被弃用,因此请改用.files[i].size
    【解决方案2】:

    I have already given an answer

    所以我这里只写代码部分

    if(empty($_FILES['file']['name'][0]))
    {
         //checking whether a single file uploaded or not
         //if enters here means no file uploaded
    }
    if(isset($_FILES['file']['name'][10]))
    {
         //checking whether 11 files uploaded or not
         //so here you can restrict user from uploading more than 10 files
    }
    

    【讨论】:

      【解决方案3】:

      对于前端,您还应该考虑使用文件上传库:它们允许限制等等:

      他们也可以通过https://cdnjs.com/获得

      【讨论】:

        猜你喜欢
        • 2011-11-15
        • 2012-04-06
        • 2011-09-28
        • 2019-12-08
        • 2017-03-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-06
        • 2021-09-21
        相关资源
        最近更新 更多