multiple image upload doesn't insert data

MowerQQ :

I'm trying to overwrite my code to accept multiple images, but somehow it doesn't work. It used to do before, with only one image, but I'd need <=5 images to upload at the same time. This is my input:

 <input type='file' name='file' multiple accept=".jpg, .png, .jpeg" />

and this is the uploader:

    if(count($_FILES) > 0) {
        if(is_uploaded_file($_FILES['file']['tmp_name'])) {
            $selectid = $conn->prepare("SELECT max(id) AS id FROM cards");
            $selectid->execute();

            $cardid = $selectid->get_result()->fetch_array()['id'];

            for($count = 0; $count < count($_FILES["file"]["tmp_name"]); $count++){
            $imgData = file_get_contents($_FILES['file']['tmp_name'][$count]);
        }
            $stmt = $conn->prepare("INSERT INTO cardimages(image, cardid) VALUES(?, ?)");
            $stmt->bind_param("si", $imgData, $cardid);
            $stmt->execute();                              

    }
    }

It doesn't upload anything. Don't really understand why.

Ronak Dhoot :

change in your form

<form action="imgupload.php" method="post" enctype='multipart/form-data'>
     <input type='file' name='file[]' multiple accept=".jpg, .png, .jpeg" />
     <!-- make it file[] instead of file -->
     <input type="submit">
</form>

in your imgupload.php loop though $_FILES['file']['tmp_name']

if(count($_FILES) > 0) {
        foreach ($_FILES['file']['tmp_name'] as $i => $tmp_name) {
            if(is_uploaded_file($_FILES['file']['tmp_name'][$i])) {

                $selectid = $conn->prepare("SELECT max(id) AS id FROM cards");
                $selectid->execute();

                $cardid = $selectid->get_result()->fetch_array()['id'];

                for($count = 0; $count < count($_FILES["file"]["tmp_name"][[$i]]); $count++){
                $imgData = file_get_contents($_FILES['file']['tmp_name'][$i]);
            }
                $stmt = $conn->prepare("INSERT INTO cardimages(image, cardid) VALUES(?, ?)");
                $stmt->bind_param("si", $imgData, $cardid);
                $stmt->execute();                              

        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=33372&siteId=1