Reputation:
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>
[removed]
$(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>');
});
});
</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
Upvotes: 0
Views: 18321
Reputation: 2526
You just remove the index from dynamic file elements, like so:
$(document).ready(function(){
$('#add_more').click(function(){
$('#image_upload').append('<br><input type="file" name="image[]" />');
});
});
There is no need to provide the indexing. Just provide an array symbol []
only. Your problem here is that you've given an index of 2 to the first dynamic file. So the file array indexes becomes 0, 2, 3 etc, so the loop will fail to iterate properly.
Upvotes: 1
Reputation: 3611
If you just want to store the paths, I would take a look at serialize()
and unserialize()
. They are made to create storeable representations of data.
Upvotes: 0
Reputation: 86
Is there a way to store the paths of say 3 images under one field in the database?
There are at least 3 ways for doing it:
Serialize PHP array before save it into your database and unserialize after select.
Save them as a string with some separator (for example: path1||path2||pathX). Implode and explode functions would be helpful.
If you are using PostgreSQL you can just use array datatype. So you will be able to manipulate them in SQL (counting, change one of them to another etc.).
But I think it's not a better way for solving this issue. Because now you want to save only a path, after a couple of days you might want to add some extra data like an ALT text, title, description, size, path to small preview etc.
So using some "user_images" table would be more convenient.
Upvotes: 0
Reputation: 17757
<form method="post" action="" enctype="multipart/form-data" id="frmImgUpload">
<input name="multiple_uploaded_files[]" type="file" multiple="true" />
</form>
foreach ($_FILES['multiple_uploaded_files']['name'] as $file)
{
print_r($file);
//use the move_uploaded_file() to move your file on your server directory.
//fire an insert query that inserts all the file names with comma separated value
}
<input type="file" name="uploaded_file[]" id="file" >
<input type="file" name="uploaded_file[]" id="file" >
<input type="file" name="uploaded_file[]" id="file" >
foreach ($_FILES['uploaded_file']['name'] as $file)
{
print_r($file);
//use the move_uploaded_file() to move your file on your server directory.
//fire an insert query that inserts all the file names with comma separated value
}
Upvotes: 2