gadgetster Posted February 14, 2014 Share Posted February 14, 2014 (edited) 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 February 14, 2014 by gadgetster Quote Link to comment https://forums.phpfreaks.com/topic/286193-php-uploading-multiple-images-and-inserting-into-database/ Share on other sites More sharing options...
jazzman1 Posted February 14, 2014 Share Posted February 14, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/286193-php-uploading-multiple-images-and-inserting-into-database/#findComment-1468871 Share on other sites More sharing options...
aosmialowski Posted February 14, 2014 Share Posted February 14, 2014 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). Quote Link to comment https://forums.phpfreaks.com/topic/286193-php-uploading-multiple-images-and-inserting-into-database/#findComment-1468879 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.