Created
August 14, 2025 14:54
-
-
Save catwhocode/de621d32b48f8ad3c22cb20912909182 to your computer and use it in GitHub Desktop.
PHP: file upload with progress bar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Upload</title> | |
<link rel="stylesheet" href="style.css"> | |
</head> | |
<body> | |
<div id="main"> | |
<h1><a href="index.html">File Upload Demo</a></h1> | |
<p>with Progress Bar</p> | |
<form id="uploadForm" enctype="multipart/form-data"> | |
<input type="file" name="fileToUpload" id="fileToUpload"> | |
<input type="submit" value="Upload" name="submit"> | |
<div id="progressBarContainer"> | |
<div id="progressBar"></div> | |
</div> | |
<div id="percentage"></div> | |
<div id="message"></div> | |
</form> | |
</div> | |
<script src="upload-progress.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#main { | |
margin: 20px; | |
padding:10px; | |
border: solid 1px silver; | |
} | |
#progressBarContainer { | |
width:300px; | |
margin-top: 20px; | |
} | |
#progressBar { | |
height: 20px; | |
width: 0px; | |
background-color: green; | |
color: white; | |
} | |
#percentage { | |
position: absolute; | |
width: 300px; | |
height: 20px; | |
color: white; | |
z-index: 999; | |
margin-top:-20px; | |
font-weight: bold; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.getElementById('uploadForm').addEventListener('submit', function(e) { | |
e.preventDefault(); | |
var formData = new FormData(this); | |
var xhr = new XMLHttpRequest(); | |
xhr.open('POST', 'upload-progress.php', true); | |
// progress tracking | |
xhr.upload.onprogress = function(event) { | |
if (event.lengthComputable) { | |
var percentComplete = (event.loaded / event.total) * 100; | |
document.getElementById('progressBar').style.width = percentComplete + '%'; | |
document.getElementById('percentage').innerHTML = Math.round(percentComplete) + '%'; | |
} | |
}; | |
// error handling | |
xhr.onload = function() { | |
if (xhr.status === 200) { | |
document.getElementById('message').innerHTML = xhr.responseText; | |
} else { | |
document.getElementById('message').innerHTML = 'Upload failed.'; | |
} | |
}; | |
xhr.send(formData); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['fileToUpload'])) { | |
$target_dir = "uploads/"; | |
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); | |
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { | |
echo htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " uploaded."; | |
} else { | |
echo "Error uploading file."; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment