Upload multiple images to the database

Yea yea I know this question has been asked before but I just would like to know how I can modify this code in order to have it upload multiple images into the database, I’ve searched up and down this forum and could not find anything that could help me so if someone has knowledge please help me out?

$fileName = $_FILES["uploaded_file"]["name"]; // The file name
$fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["uploaded_file"]["type"]; // The type of file it is
$fileSize = $_FILES["uploaded_file"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["uploaded_file"]["error"]; // 0 for false... and 1 for true

$fileExt = end($kaboom); 

if (!$fileTmpLoc) { // if file not chosen
echo "ERROR: Please browse for a file before clicking the upload button."; exit(); 
else if($fileSize > 5242880) { 
// if file size is larger than 5 Megabytes
echo "ERROR: Your file was larger than 5 Megabytes in size."; unlink($fileTmpLoc); 
// Remove the uploaded file from the PHP temp folder
exit(); } else if (!preg_match("/.(gif|jpg|png|mp4)$/i", $fileName) ) { 
// This condition is only if you wish to allow uploading of specific file types
echo "ERROR: Your image was not .gif, .jpg, or .png."; unlink($fileTmpLoc); 
// Remove the uploaded file from the PHP temp folder
exit(); } else if ($fileErrorMsg == 1) { 
// if file upload error key is equal to 1
echo "ERROR: An error occured while processing the file. Try again."; exit(); } 

$moveResult = move_uploaded_file($fileTmpLoc, "uploads/$fileName"); 
// Check to make sure the move result is true before continuing
if ($moveResult != true) { 
    echo "ERROR: File not uploaded. Try again."; exit(); 

it depends on how you named the multiple uploads, but generally a loop will be involved.

there is also an excellent image upload example in the PHP Manual.

You can name your file as array

<input type="file" name="uploaded_file[]">
<input type="file" name="uploaded_file[]">
// or, HTML5 version (select multiple files)
<input type="file" name="uploaded_file[]" multiple="multiple">

and you should have a POST like
[name][0], [name][1]
[size][0], [size][1]
[tmp_name][0], [tmp_name][1]


I feel like I didn’t explain it clear enough but, what I want to do is have just one file field to upload multiple images to the database and have those images still link back to the users id, don’t worry about the linking back part I already know how to do that but having it upload multiple images is the annoying part. what it’s doing is taking the last image that was selected so for example if I selected image1.png, image2.png or image3.png it will only upload image3.png and ignore the first two…does that make sense? and thank you for the links guys :slight_smile: will take a look at them. I forgot to include this but this is how they’re being handled:

<input type="file" name="uploaded_file" id="uploaded_file" multiple="multiple" autocomplete="off">

I think the only way to do this would be to have the users zip the images and then upload the zip file.

Much easier to have multiple file inputs. Why do you want only one?

As I said, try to name your field name="uploaded_file[]" and


there is an attribute called multiple … see http://www.w3.org/wiki/HTML/Elements/input/file

1 Like

Does this code help? http://stackoverflow.com/questions/24895170/multiple-image-upload-php-form-with-one-input

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.