简体   繁体   中英

All images not uploading in multiple images upload

I'm trying to upload multiple files and it uploads them well. But I am also checking for errors (type and size) and catch errors in an array variable. When I select, lets say, 3 images and one of them has some error (more size than allowed and/or type not allowed) and its the first image then the other two images also don't get uploaded. If the file with error is second one then only first one gets uploaded, and when error image is last one the first two get uploaded. What I'm trying to do is even if there is error in one or more images then other valid images should get uploaded no matter the order in which they are selected.

Here is my script:

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;
        }
    }

}

In the loop you have checked $error[] . So when the error is in first file so that array will not be blank and other images will not upload.

Try as below :

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;
    }
}

I have changed error message array from $errors[] to $errors[$key] and checked the same. So if you have 4 file input and 1st and 3rd are having error then 2nd and 4th will upload and you will get error in 0th and 2nd index of array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM