0

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>
GYaN
  • 2,327
  • 4
  • 19
  • 39
Jack Larson
  • 184
  • 1
  • 10
  • >> [Possible Duplicate](https://stackoverflow.com/questions/49743524/how-to-bind-varying-number-of-inputs-when-some-are-blob-and-must-be-sent-send-lo) – Karlo Kokkak Apr 28 '18 at 08:54
  • 1
    If the images are more than about 100k, then this isn't such a great idea – Strawberry Apr 28 '18 at 09:04

1 Answers1

0

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.