简体   繁体   中英

How to upload multiple images in php and store it as blob mysql

Here is my current code and can anyone help me how i can upload multiple images with a text in category? i have to store the images as blob in database.

<?php
    if(count($_FILES) > 0) {
        if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
            mysql_connect("localhost", "root", "");
            mysql_select_db ("au");
            $cat = $_POST['cat'];
            $imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
            $imageProperties = getimageSize($_FILES['userImage']['tmp_name']);
            $sql = "INSERT INTO output_images(imageType ,imageData, category)
            VALUES('{$imageProperties['mime']}', '{$imgData}', '$cat')";
            $current_id = mysql_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysql_error());
            if(isset($current_id)) {
                header("Location: listImages.php");
            }
        }
    }
?>
<HTML>
    <HEAD>
        <TITLE>Upload Image to MySQL BLOB</TITLE>
        <link href="imageStyles.css" rel="stylesheet" type="text/css" />
    </HEAD>
    <BODY>
        <form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload">
            <label>Upload Image File:</label><br/>
            <input type="text" name="cat">
            <input name="userImage" type="file" class="inputFile" />
            <input type="submit" value="Submit" class="btnSubmit" />
        </form>
    </div>
    </BODY>
</HTML>

I have two articles in my blog refer to multiple files uploading and storing images in MySQL database.

  • Multiple files uploading

upload.html

<form action="product.php" method="post" enctype="multipart/form-data">
  <input type="file" class="form-control-file" name="prod_pic[]" id="prod_pic[]">
  <input type="file" class="form-control-file" name="prod_pic[]" id="prod_pic[]">
             :
  <button type="submit" class="btn btn-primary">Save Prodcut Info</button>
</form>

Note that the 'name' property of the <input> tag should be added an array tag '[]' . In addition, enctype="multipart/form-data" should be added in the tag in order to enable the file uploading function. Thus, the $_FILES variable can be an array to carry multiple files information.

Then, the product.php converts $_FILE variable and save files to the destination path.

product.php

<?php
  if (isset($_FILES['prod_pic'])) {
    $pics = convert_upload_file_array($_FILES['prod_pic']);

    foreach ($pics as $key => $pic) {
      $target = "images/{$pic['name']}";
      move_uploaded_file($pic['tmp_name'], $target);
    }
  }
?>

$_FILES variable as an array is not as a usual form as we think. It is formed by using file's attribute as the key, not the index number. Therefore, we need a converting function convert_upload_file_array() .

function convert_upload_file_array($upload_files) {
  $converted = array();

  foreach ($upload_files as $attribute => $val_array) {
    foreach ($val_array as $index => $value) {
      $converted[$index][$attribute] = $value;
    }
  }
  return $converted;
}

Read more: How to Upload Multiple Files in PHP

  • Regarding storing images in MySQL database

Please refer to the following code:

$prod_pic = $mysqli->real_escape_string(file_get_contents($_FILES['prod_pic']['tmp_name']));
$prod_pic_type = $_FILES['prod_pic']['type'];
 :
 :
$sql = "UPDATE products 
        SET prod_pic      = '{$prod_pic}',
            prod_pic_type = '{$prod_pic_type}'
        WHERE prod_id = {$prod_id}";
$mysqli->query($sql) or die($mysqli->connect_error);
$mysqli->close(); 

The key part is As you can see, the key part is:

file_get_contents($_FILES['prod_pic']['tmp_name'])

and

$mysqli->real_escape_string(......)

Read more: Insert and Update An Image to MySQL Server Field with Blob Data Type in PHP

By using the key ideas above, your goal should be easily achieved.

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