Multiple File Upload in PHP With Database
- Article
- Comment
Uploading Multiple Files Together in one time is a bit advanced than single file upload. My Previous Article File Upload in PHP With Database, Describes you to handle single file upload. Here I am going to use that article and create Multiple File Upload in PHP with Database.
The Previous one, you can’t select multiple files with the form, the form doesn’t allow you to select multiple files. Let’s change the HTML form to allow the user to select multiple files at the same selection window.
HTML Form
HTML form needs to pass one more additional parameter with it. Here I have changed the color of the text which I am changing from the previous article.
<!-- form.php --> <form action="uploads.php" method="post" enctype="multipart/form-data"> <label> Select File: <input type="file" name="attachment[]" multiple > </label> <input type="submit" name="HandleUpload" value="Upload" > </form>
Now, Let’s write the PHP function to handle the multiple files.
Multiple File Upload
Let’s Create the PHP function now.
if(count($_FILES['attachment']['name']) > 0) { $Kv = 0; $uploads_dir = "uploads/"; foreach($_FILES['attachment']['name'] as $filename) { move_uploaded_file($_FILES["attachment"]["tmp_name"][$Kv], $uploads_dir. basename($filename)); $Kv++; } }
The Above one without the inserting query. Let’s go in deeper with file Validations and inserting it into the database.
Size Restriction
Let’s check the file size, which doesn’t go more than 2MB with below code.
$maxsize = 2 * 1024 * 1024; //2MB maximum allowed. foreach($_FILES['attachment']['size'] as $size) { if($size > $maxsize) die("Error: File size is larger than the allowed limit."); }
And Let’s check the File Type.
File Type
File Type can be checked by looping all the uploaded files.
$allowed = array( 'jpg', 'jpeg', 'gif', 'png', 'bmp'); foreach($_FILES['attachment']['name'] as $name) { $type = pathinfo($name, PATHINFO_EXTENSION); if(!in_array($type, $allowed)) die("Error: Please select a valid file format.-".$type); }
The Above one has an array of allowed types and if any file which doesn’t in the array, than the total upload will be canceled. We Already Created a database and followed by a table from the previous article, we can use that here as well. So i am skipping the part of creating database and table. If you want to get the details, please use the above link and get it.
Inserting Database
We have to insert each uploaded files. Let’s Do it with loop.
$Kv_items = array(); $Kv = 0; $uploads_dir = "uploads/"; foreach($_FILES['attachment']['name'] as $filename) { move_uploaded_file($_FILES["attachment"]["tmp_name"][$Kv], $uploads_dir. basename($filename)); mysqli_query($kv, "INSERT INTO kv_images (filename, type, size, date) values ( '".$_FILES["attachment"]["name"][$Kv]."', '".$_FILES["attachment"]["type"][$Kv]."', '".$_FILES["attachment"]["size"][$Kv]."', '".date('Y-m-d')."')"); if(mysqli_insert_id($kv)){ $Kv++; $Kv_items[] = mysqli_insert_id($kv); } } if(count($Kv_items)) echo count($Kv_items).' Files Inserted Successfully!' ;
And now, you have learned how to work with multiple files. Also I have the download for the source code which will be helpful for you to test it.
Thanks for Reading it. If you enjoy reading this article, You can also share it on social medias and also you can bookmark it for further reference. If you are interested to get more updates from me, just follow me on the below social medias and also subscribe me to get new articles via e-mail.