Vanilla JavaScript Multiple File Upload with Progress bar using Ajax with PHP. In this tutorial, we will show you how to upload multiple file with progress bar using pure vanilla JavaScript with Ajax & PHP.
When we have upload file on to the server, so on web page we can not check the file uploading progress, and it is very difficult for user to check the file upload process. So at time Progress bar or progress meter is very useful feature, because it has reduce the issue of displaying multiple file upload process or progress on web page in graphical form. Because A Progress bar or Progress meter is a graphical element which has display live uploading progress on web page. So mainly Progress bar has bee used for display progress process in percentage format at the time when we have upload or download or install any software. So under this tutorial, we will show you how to upload multiple file with progress bar using JavaScript & Ajax with PHP script.
If you have made web application then under Web based application we need to upload file to server, so at that time we need to display progress bar while uploading of multiple image file in PHP. So from this tutorial, you can find the solution of how to display multiple image file upload progress in progress bar meter using JavaScript & Ajax with PHP script. Under this tutorial, we will use Pure Vanilla JavaScript for send selected multiple files to server at client-side and for upload multiple files to server, we will use PHP script at server-side.
Under this tutorial, we have use following technology for Upload Multiple file with Progress bar using JavaScript with PHP.
- HTML
- Bootstrap 5 Stylesheet Only
- Vanilla JavaScript
- Ajax
- PHP
Source Code
index.php
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Multiple File Upload with Progress Bar using JavaScript & PHP</title>
</head>
<body>
<div class="container">
<h1 class="mt-3 mb-3 text-center">Multiple File Upload with Progress Bar using JavaScript & PHP</h1>
<div class="card">
<div class="card-header">Select File</div>
<div class="card-body">
<table class="table">
<tr>
<td width="50%" align="right"><b>Select File</b></td>
<td width="50%">
<input type="file" id="select_file" multiple />
</td>
</tr>
</table>
</div>
</div>
<br />
<div class="progress" id="progress_bar" style="display:none; ">
<div class="progress-bar" id="progress_bar_process" role="progressbar" style="width:0%">0%</div>
</div>
<div id="uploaded_image" class="row mt-5"></div>
</div>
</body>
</html>
<script>
function _(element)
{
return document.getElementById(element);
}
_('select_file').onchange = function(event){
var form_data = new FormData();
var image_number = 1;
var error = '';
for(var count = 0; count < _('select_file').files.length; count++)
{
if(!['image/jpeg', 'image/png', 'video/mp4'].includes(_('select_file').files[count].type))
{
error += '<div class="alert alert-danger"><b>'+image_number+'</b> Selected File must be .jpg or .png Only.</div>';
}
else
{
form_data.append("images[]", _('select_file').files[count]);
}
image_number++;
}
if(error != '')
{
_('uploaded_image').innerHTML = error;
_('select_file').value = '';
}
else
{
_('progress_bar').style.display = 'block';
var ajax_request = new XMLHttpRequest();
ajax_request.open("POST", "upload.php");
ajax_request.upload.addEventListener('progress', function(event){
var percent_completed = Math.round((event.loaded / event.total) * 100);
_('progress_bar_process').style.width = percent_completed + '%';
_('progress_bar_process').innerHTML = percent_completed + '% completed';
});
ajax_request.addEventListener('load', function(event){
_('uploaded_image').innerHTML = '<div class="alert alert-success">Files Uploaded Successfully</div>';
_('select_file').value = '';
});
ajax_request.send(form_data);
}
};
</script>
upload.php
<?php
//upload.php
if(isset($_FILES['images']))
{
for($count = 0; $count < count($_FILES['images']['name']); $count++)
{
$extension = pathinfo($_FILES['images']['name'][$count], PATHINFO_EXTENSION);
$new_name = uniqid() . '.' . $extension;
move_uploaded_file($_FILES['images']['tmp_name'][$count], 'images/' . $new_name);
}
echo 'success';
}
?>
Hello / Thank you for your training
ReplyDeletePlease post a tutorial on how to make an ID card with PHP and jQuery working Drag and drop on dynamic fields on the site