如何获取所有上传的文件大小?

时间:2017-06-08 09:29:07

标签: jquery codeigniter

我有七个输入文件来附加图像。当我点击提交按钮时,上传文件大小将限制为10 MB(包括所有图像)。

<input name="a" id="file-0" type="file" class="dropify"/>
<input name="b" id="file-0" type="file" class="dropify"/>
<input name="c" id="file-0" type="file" class="dropify"/>

2 个答案:

答案 0 :(得分:0)

$('form').submit(function( e ) {    
    if(!($('#file')[0].files[0].size < 10485760 && get_extension($('#file').val()) == 'jpg')) { // 10 MB (this size is in bytes)
        //Prevent default and display error
        alert("File is wrong type or over 10Mb in size!");
        e.preventDefault();
    }
});

<form method="post" enctype="multipart/form-data" action="upload.php">
    <input type="file" name="file" id="file" multiple/>
    <input type="submit" name="submit" value="Submit" />
</form>

<script>
document.forms[0].addEventListener('submit', function( evt ) {
    var file = document.getElementById('file').files[0];

    if(file && file.size < 10485760) { // 10 MB (this size is in bytes)
        //Submit form        
    } else {
        //Prevent default and display error
        evt.preventDefault();
    }
}, false);
</script>

小提琴: http://jsfiddle.net/go4vaibhav/msuL0yta/33/

答案 1 :(得分:0)

public function do_upload()
{
        $config['upload_path']          = './uploads/';
        $config['allowed_types']        = 'gif|jpg|png';
        $config['max_size']             = 100;
        $config['max_width']            = 1024;
        $config['max_height']           = 768;

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload('userfile'))
        {
                $error = array('error' => $this->upload->display_errors());

                $this->load->view('upload_form', $error);
        }
        else
        {
                $data = array('upload_data' => $this->upload->data());

                $this->load->view('upload_success', $data);
        }
}   

这是一个辅助方法,它返回一个数组,其中包含与您上传的文件相关的所有数据。这是数组原型:

Array
(
    [file_name]     => mypic.jpg
    [file_type]     => image/jpeg
    [file_path]     => /path/to/your/upload/
    [full_path]     => /path/to/your/upload/jpg.jpg
    [raw_name]      => mypic
    [orig_name]     => mypic.jpg
    [client_name]   => mypic.jpg
    [file_ext]      => .jpg
    [file_size]     => 22.2
    [is_image]      => 1
    [image_width]   => 800
    [image_height]  => 600
    [image_type]    => jpeg
    [image_size_str] => width="800" height="200"
)

从数组中返回一个元素:

$this->upload->data('file_size');       // File size in kilobytes