简体   繁体   中英

Upload multiple images to Database MYSQLI

I am planning to have a web gallery. However, it is hard for me to use PHP to insert DB. The following code:

HTML -- I want to make a form which has category and multiple images that can be inserted into DB at the same time.

<form action="upload" method="POST" enctype="multipart/form-data">
    <p> Upload : <input type="file" id="file" name="images" /> </p>
    <p> Category : <input type="text" name="imageCategory"> </p>
    <p> <input type="submit" name="submit" value="Upload!" /> </p>
</form>

DATABASE

I am using imageName as VARCHAR not BLOB TYPE.

PHP

<?php
include ("dbConnect.php");

    if(isset($_POST["submit"])) {

        $image = $_POST['images']['tmp_name'];
        $imageName = $_POST['images']['name'];
        $imageSize = $_POST['images']['size'];
        $imageType = $_POST['images']['type'];
        $imageCategory = $_POST['imageCategory'];

        $result =   $mysqli->query("INSERT INTO imageTable (imageName, imageCategory, imageSize, imageType) 
                                  VALUES ('$imageName', '$imageCategory', '$imageSize' , '$imageType' );")

                    or die(mysqli_error($mysqli));

    } else {

        echo "<p> It is not working </p>";

    }

    header("location: index"); 

    $mysqli->close();
?>

The problem is, the category is the only one has inserted into the database successfully. But not with the imageName, imageType, and imageSize. And also i want the image to be stored into database so that I can retrieve the image from DB on the other web page. Any ideas?

You can use 'multiple' property in the 'input' tag like this :

<form action="file-upload.php" method="post" enctype="multipart/form-data">
  Send these files:<br />
  <p> <input name="userfile[]" type="file" multiple='multiple' /> </p>
  <p> Category : <input type="text" name="imageCategory"> </p>
  <input type="submit" value="Send files" />
</form>

User can select multiple files and upload them. And at the server you will do this :

if (isset($_FILES["userfile"]) && !empty($_FILES["userfile"])) {
        $image = $_FILES['userfile']['tmp_name'];
        $imageName = $_FILES['userfile']['name'];
        $imageSize = $_FILES['userfile']['size'];
        $imageType = $_FILES['userfile']['type'];
        $imageCategory = $_POST['imageCategory'];
        $len = count($image);
        $path = "images/";
        for ($i = 0; $i < $len; $i++) {
             if (isset($imageName[$i]) && $imageName[$i] !== NULL) {
                 if(move_uploaded_file($image[$i], $path.$imageName[$i])) {
                    $result = $mysqli->query("INSERT INTO imageTable (imageName, imageCategory, imageSize, imageType) VALUES ('$imageName[$i]', '$imageCategory', '$imageSize[$i]' , '$imageType[$i]' )");
                 }
             }
        }
}
$mysqli->close();
header("location: index"); 

First off the file information won't be in the $_POST global variable it will be in the $_FILES

From http://php.net/manual/en/features.file-upload.multiple.php (modified):

Form

<form action="file-upload.php" method="post" enctype="multipart/form-data">
  Send these files:<br />
  <p> <input name="userfile[]" type="file" /> </p>
  <p> <input name="userfile[]" type="file" /> </p>
  <p> Category : <input type="text" name="imageCategory"> </p>
  <input type="submit" value="Send files" />
</form>

Parse the global file variable to an array (we'll assume this is a helper function called parseFiles.php):

<?php

function reArrayFiles(&$file_post) {

    $file_ary = array();
    $file_count = count($file_post['name']);
    $file_keys = array_keys($file_post);

    for ($i=0; $i<$file_count; $i++) {
        foreach ($file_keys as $key) {
            $file_ary[$i][$key] = $file_post[$key][$i];
        }
    }

    return $file_ary;
}

Move the files and insert the file information into the database :

<?php

include ("dbConnect.php");
include ("parseFiles.php");

if ($_FILES['upload']) {
    $file_ary = reArrayFiles($_FILES['userfile']);

    foreach ($file_ary as $file) {

        $image = $file['tmp_name'];
        $imageName = $file['name'];
        $imageSize = $file['size'];
        $imageType = $file['type'];
        $imageCategory = $_POST['imageCategory'];

        $result =   $mysqli->query("INSERT INTO imageTable (imageName, imageCategory, imageSize, imageType) 
                                  VALUES ('$imageName', '$imageCategory', '$imageSize' , '$imageType' );")

                    or die(mysqli_error($mysqli));
    }

} else {

    echo "<p> It is not working </p>";

}

header("location: index"); 

$mysqli->close();

Hopefully that should do the job. I've not tested this I've just blind coded it so there may be some bugs

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