多张图片上传中所有图片未上传

All images not uploading in multiple images upload

我正在尝试上传多个文件,它上传得很好。但我也在检查错误(类型和大小)并在数组变量中捕获错误。当我 select,比方说,有 3 张图片,其中一张有一些错误(尺寸超过允许 and/or 类型不允许)并且它是第一张图片,然后其他两张图片也没有上传.如果有错误的文件是第二个,那么只有第一个被上传,当错误图像是最后一个时,前两个被上传。我想做的是,即使一张或多张图片有错误,其他有效图片也应该上传,无论它们 selected.

的顺序如何

这是我的脚本:

function filesupload($files) // here files is $_FILES array
{

    $i = 0;
    $errors = array();
    $maxfilesize = 1*1024*1024; // 1 MB
    $num = count($files['name']);
    $allowed_types = array('image/jpeg', 'image/png');

    foreach($files['tmp_name'] as $key=>$tmp_name)
    {
        $tmpname = $files['tmp_name'][$key]; // file temp name
        $fsize = $files['size'][$key];  // file size

        if(!empty($files['name'][$key]))
        {
            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $ftype = finfo_file($finfo, $files['tmp_name'][$key]); // file mime type
        }

        //validations for file type and size

        // no file selected
        if(empty($files['name'][$key]))
        {
            $errors[] = 'Select at least one file for uploading';
        }

        // file type not allowed
        if(in_array($ftype, $allowed_types) === false)
        {
            $errors[] = 'One or more files have invalid file extension';
        }

        // file size validation
        if($fsize > $maxfilesize)
        {
            $errors[] = 'Size of one or more files is more than allowed';
        }

        // if no errors uploaded file(s)
        if(empty($errors))
        {
            $path = 'images/';
            $newfilename = time().'_'.rand(100000, 999999).'_'.$files['name'][$key];
            $move = move_uploaded_file($tmpname, $path.$newfilename);
            if($move)
            {
                $i = $i + 1;
                if($i == $num)
                {
                    $msg = 'Files uploaded';
                    return $msg;
                }
            }
        }
        elseif(!empty($errors))
        {
            return $errors;
        }
    }

}

在循环中您已检查 $error[]。所以当错误在第一个文件中时,数组将不会为空,其他图像将不会上传。

尝试如下:

function filesupload($files) // here files is $_FILES array
{
    $i = 0;
    $errors = array();
    $maxfilesize = 1*1024*1024; // 1 MB
    $num = count($files['name']);

    $allowed_types = array('image/jpeg', 'image/png');

    foreach($files['tmp_name'] as $key=>$tmp_name)
    {
        $tmpname = $files['tmp_name'][$key]; // file temp name
        $fsize = $files['size'][$key];  // file size

        if(!empty($files['name'][$key]))
        {
            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $ftype = finfo_file($finfo, $files['tmp_name'][$key]); // file mime type
        }

        //validations for file type and size

        // no file selected
        if(empty($files['name'][$key]))
        {
            $errors[$key] = 'Select at least one file for uploading';
        }

        // file type not allowed
        if(in_array($ftype, $allowed_types) === false)
        {
            $errors[$key] = 'One or more files have invalid file extension';
        }

        // file size validation
        if($fsize > $maxfilesize)
        {
            $errors[$key] = 'Size of one or more files is more than allowed';
        }

        // if no errors uploaded file(s)
        if(!isset($errors[$key]))
        {
            $path = 'images/';
            $newfilename = time().'_'.rand(100000, 999999).'_'.$files['name'][$key];
            $move = move_uploaded_file($tmpname, $path.$newfilename);
            if($move)
            {
                $i = $i + 1;
                if($i == $num)
                {
                    $msg = 'Files uploaded';
                    return $msg;
                }
            }
        }
    }

    if(!empty($errors))
    {
        return $errors;
    }
}

我已将错误消息数组从 $errors[] 更改为 $errors[$key] 并进行了检查。因此,如果您有 4 个文件输入并且第 1 个和第 3 个有错误,那么第 2 个和第 4 个将上传并且您将在数组的第 0 个和第 2 个索引中得到错误。