Multiple File Upload in PHP
Sometimes we have to upload multiple files on the website. For example, n the Event website where there is a need to upload event gallery images category-wise. Like Holi events, new year event images frequently, that time multiple image upload is necessary. Because if we upload images one by one, it will take a lot of time. So to resolve this problem, we use the PHP multiple file upload features. Here are some steps following:
Step 1: Create HTML FORM<form method="POST" enctype="multipart/form-data">
<input type="file" class="form-control" name="doc[]" multiple>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
1. []: In this file input, we have used array symbol; array symbol is used to store more than one value in the input.
2. multiple: In this file input, we have used multiple attributes; multiple attributes allow you to select more than one file to upload.
Step 2 : PHP Code
<?php
if(isset($_POST['submit']))
{
$length= count($_FILES['doc']['name']);
for($i=0;$i<$length;$i++)
{
$file_name = $_FILES['doc']['name'][$i];
$file_tmp = $_FILES['doc']['tmp_name'][$i];
move_uploaded_file($file_tmp,"uploads/".$file_name);
}
}
?>
Complete Code:
<?php
if(isset($_POST['submit'])){
$length= count($_FILES['doc']['name']);
for($i=0;$i<$length;$i++)
{
$file_name = $_FILES['doc']['name'][$i];
$file_tmp = $_FILES['doc']['tmp_name'][$i];
move_uploaded_file($file_tmp,"uploads/".$file_name);
}
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="doc[]" multiple>
<input type="submit" name="submit">
</form>
</body>
</html>