I want to upload multiple images in one single column.
In my PHP form I have an input for name, upload images button and submit.
I have a database table named my_images
with columns id, name, and image_data.
In my form, I input a name and I want to upload multiple images and only insert it in a single row in my database my_image
table upload image form:
<h2>Upload Images</h2>
<form method="post" enctype="multipart/form-data">
<label for="name">Name:</label>
<input type="text" name="name" id="name"><br><br>
<label for="images[]">Images:</label>
<input type="file" name="images[]" id="images" multiple><br><br>
<input type="submit" value="Upload">
</form>
<?php
// Check if the form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the name entered by the user
$name = $_POST["name"];
// Initialize an array to hold the image data
$image_data_array = array();
// Loop through each uploaded image
foreach ($_FILES["images"]["tmp_name"] as $key => $tmp_name) {
// Check if the image was uploaded successfully
if ($_FILES["images"]["error"][$key] == UPLOAD_ERR_OK) {
// Read the contents of the image file
$image_data = file_get_contents($tmp_name);
// Add the image data to the array
$image_data_array[] = $image_data;
} else {
echo "Error uploading image: " . $_FILES["images"]["error"][$key];
}
}
// Combine the image data into a single binary string
$combined_image_data = implode("", $image_data_array);
$stmt = mysqli_prepare($conn, "INSERT INTO my_images (name, image_data) VALUES (?, ?)");
mysqli_stmt_bind_param($stmt, "sb", $name, $combined_image_data);
mysqli_stmt_execute($stmt);
echo "Images uploaded successfully!";
}
?>
I tried can combine the image data into a single binary string and store it in a BLOB column, but this only put the images in multiple columns.
And I don’t want to make multiple input fields for my images database file when I submit multiple imges:
the reason why i want to do this is because in my website
i want to display containers with a name and multiple images
container with images
2
Answers
You want to put all the images into one column, which could be done. but it is probably not the best thing to do.
might I suggest, upload the images into a folder. then you can concatenate the paths of the images into one string and put this in one column.
you would then have to deconstruct the string to give you the individual paths to the images, which you would then feed into the html so you can display the images as you needed.
you could also just create a table the for images and a table for the user
and then create a relationship between them as to identify which images belong to which user.
these methods are going to be less cpu intensive and less convoluted then the method your are proposing.
It’s possible, but I don’t recommend this method.
First of all I point out the data type is better indicated as string, so:
should be:
So to retrieve the single images you have to save somewhere the number and the sizes of them. To achieve this you could do this way:
Then you need to store a header in the blob before the image data:
Finally you can store the data
To retrieve the images you must read the header to know the number and the sizes of the images to split the blob:
But what I recommend is to create two tables: the first we call the parent table containing an id and description, the second contains a row for each image with any details and also the id of the parent table:
In this way, to retrieve the images you can select all the rows with my_image details.my_image _id equal to my images.id and every row will contain a single image
Finally, I also suggest that you evaluate the possibility of storing the images on the file system and always using two tables, to store in the child table the image path instead of its content