Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 显示多个上载文件的组合大小_Javascript_Php_Jquery_Html_File Upload - Fatal编程技术网

Javascript 显示多个上载文件的组合大小

Javascript 显示多个上载文件的组合大小,javascript,php,jquery,html,file-upload,Javascript,Php,Jquery,Html,File Upload,我正在用php制作一个上传函数,效果很好。 只是,因为我的PHPINI文件的最大大小是32mb,所以我不能上传比这个更大的文件。这对我来说很好 但是: 当添加到输入文件的所有文件的组合大小超过32mb时,我希望阻止上载 我发现这个脚本在添加文件后会提醒文件大小,这是一个开始: $(function(){ $('#file').change(function(){ var file=this.files[0] alert(file.size||file.fi

我正在用php制作一个上传函数,效果很好。 只是,因为我的PHPINI文件的最大大小是32mb,所以我不能上传比这个更大的文件。这对我来说很好

但是
当添加到输入文件的所有文件的组合大小超过32mb时,我希望阻止上载

我发现这个脚本在添加文件后会提醒文件大小,这是一个开始:

$(function(){
    $('#file').change(function(){
        var file=this.files[0]
        alert(file.size||file.fileSize)
    })
})
但是在测试时,我发现它只返回一个文件的大小。 如何更改此代码以返回添加到输入字段的所有文件的大小?还是有其他方法可以做到这一点

HTML:


辅导的

PHP代码:

$file_dest = "photos/orig/";
$thumb_dest = "photos/thumbs/";

if(!empty($_FILES['files']['name'][0])){

    $files = $_FILES['files'];

    $uploaded = array();
    $failed = array();

    $allowedExt = array('png', 'jpg', 'gif');

    foreach($files['name'] as $position => $file_name) {
        $file_tmp = $files['tmp_name'][$position];
        $file_size = $files['size'][$position];
        $file_error = $files['error'][$position];

        $file_ext = explode('.', $file_name);
        $file_ext = strtolower(end($file_ext));

        if(in_array($file_ext, $allowedExt)){

            if($file_error == 0){

                if($file_size <= 20000000){

                    $file_name_new = uniqid('', true)."_".$file_name;
                    $file_move = $file_dest.$file_name_new;

                    if(move_uploaded_file($file_tmp, $file_move)){
                        $uploaded[$position] = $file_dest;

                    }else{
                        $failed[$position] = "[{$file_name}] failed to upload.";
                    }

                }else{
                    $failed[$position] = "[{$file_name}] is too large.";
                }

            }else{
                $failed[$position] = "[P$file_name}] errored with code {$file_error}";
            }

        }else{
            $failed[$position] = "[{$file_name}] file extension '{$file_ext}' is not allowed.";
        }
    }

    if(!empty($uploaded)){
        print_r($uploaded);
    }

    if(!empty($failed)){
        print_r($failed);
    }
}else{
    echo "No files were added.";
}
$file_dest=“photos/orig/”;
$thumb_dest=“照片/拇指/”;
如果(!空($_FILES['FILES']['name'][0])){
$files=$_文件['files'];
$upload=array();
$failed=array();
$allowedExt=array('png','jpg','gif');
foreach($files['name']作为$position=>$file\u name){
$file_tmp=$files['tmp_name'][$position];
$file_size=$files['size'][$position];
$file_error=$files['error'][$position];
$file\u ext=explode('.',$file\u name);
$file_ext=strtolower(end($file_ext));
if(在数组中($file\u ext,$allowedExt)){
如果($file\u error==0){
if($file\u size
$(函数(){
$('#file').change(函数(){
var combinedSize=0;

对于(var i=0;i您可以通过将大小相加来实现

$(function(){
    $('#file').on('change', function(){
        var total = [].slice.call(this.files).map(function(x) {
            return x.size || x.fileSize;
        }).reduce(function(a, b) { return a + b; }, 0);

        if ( total > 33554432 ) {
            alert('Total size exceeds 32 mb');
        }

    });
});

$(function(){
    $('#file').change(function(){
        var combinedSize = 0;
        for(var i=0;i<this.files.length;i++) {
            combinedSize += (this.files[i].size||this.files[i].fileSize);
        }
        alert(combinedSize);
    })
})
$(function(){
    $('#file').on('change', function(){
        var total = [].slice.call(this.files).map(function(x) {
            return x.size || x.fileSize;
        }).reduce(function(a, b) { return a + b; }, 0);

        if ( total > 33554432 ) {
            alert('Total size exceeds 32 mb');
        }

    });
});