Welcome Guest, Not a member yet? Register   Sign In
PHP uploading multiple images and inserting into database
#1

[eluser]Unknown[/eluser]
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...

Code:
<ul id="image_upload">
    &lt;input type="file" name="image[]"&gt;&lt;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>&lt;input type="file" name="image[' + next_count + ']"&gt;&lt;br>');
       });
      });
     [removed]


</ul> <br><br>

then i check the files with php and attempt to insert to the database
Code:
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




Theme © iAndrew 2016 - Forum software by © MyBB