Jump to content

PHP uploading multiple images and inserting into database


Recommended Posts

i want the user to be able to upload multiple images. Is there a way to store the paths of say 3 images under one field in the database? I mean having all of the image paths in the same column like so
Images                                                                                       | date
images/file1.jpg, images/file2.jpg, images/file3.jpg                    | Thursday...
 
<ul id="image_upload">
<input type="file" name="image[]"><a href="#" id="add_more">Add more</a> <br>
<script>
$(document).ready(function(){
$('#add_more').click(function(){
var current_count = $('input[type="file"]').length;
var next_count = current_count + 1;
$('#image_upload').append('<br><input type="file" name="image[' + next_count + ']"><br>');
});
});
</script>

</ul> <br><br>
 
 
 
 
then i check the files with php and attempt to insert to the database
if (!empty($_FILES['image'])){  
for ($i = 0; isset($_FILES['image']['name'][$i]); $i++) {
$allowed = array('jpg', 'jpeg', 'png', 'bmp', 'tiff', 'gif');
$fileName = $_FILES['image']['name'][$i];
$fileSize = $_FILES['image']['size'][$i];
$fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
 
if (!in_array($fileExt, $allowed)) {
$errors[] = 'Incorrect file type. Only allowed: ' . implode(', ', $allowed) . '';
}
if ($fileSize > 2097152) {
$errors[] = "$fileName exceeds the maximum file size of 2 MB";
}
}
 
for ($i = 0; isset($_FILES['image']['name'][$i]); $i++) {
$fileBase = basename($_FILES['image']['name'][$i]);
$fileName = pathinfo($fileBase, PATHINFO_FILENAME);
$fileExt = pathinfo($fileBase, PATHINFO_EXTENSION);
$fileTmp = $_FILES['image']['tmp_name'][$i];
$fileDst = 'images/images/'.basename($_FILES['image']['name'][$i]);
for ($j = 0; file_exists($fileDst); $j++) {
$fileDst = "images/images/$fileName-$j.$fileExt";
}
if (move_uploaded_file($fileTmp, $fileDst)) {
$output[$fileBase] = "Stored $fileBase OK";
} 
}
}

 

this code only uploads the first image to the temp folder and only one path into the database

Edited by gadgetster

 

Is there a way to store the paths of say 3 images under one field in the database?

 

1) Database tables haven't fields, just rows and columns. Columns are not contained in fields.

 

 

for ($i = 0; isset($_FILES['image']['name'][$i]); $i++)

 

2) I cannot see what the condition in second expression of this loop is?

 

 

I mean having all of the image paths in the same column like so......

 

3) That is one way NOT to do it. What would happened if you decide to update or delete this row/column at some point?

You should avoid storing multiple values in single database column. It causes problems. You should also avoid storing images in database as it may grows fastly. You should consider storing only images path in a separate table connected (an assiociation with main table).

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.