Files
random_scripts/php_upload/upload.php
T
2024-07-04 17:12:58 +01:00

95 lines
4.0 KiB
PHP

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Upload</title>
<!-- Bootstrap CSS -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<h2>Upload a Large File</h2>
<?php
// Get configuration limits
$upload_max_filesize = ini_get('upload_max_filesize');
$post_max_size = ini_get('post_max_size');
$max_execution_time = ini_get('max_execution_time');
$memory_limit = ini_get('memory_limit');
?>
<p><strong>Maximum Upload Size:</strong> <?php echo $upload_max_filesize; ?></p>
<p><strong>Maximum POST Size:</strong> <?php echo $post_max_size; ?></p>
<p><strong>Maximum Execution Time:</strong> <?php echo $max_execution_time; ?> seconds</p>
<p><strong>Memory Limit:</strong> <?php echo $memory_limit; ?></p>
<form id="uploadForm" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="fileToUpload">Select file to upload:</label>
<input type="file" class="form-control" name="fileToUpload" id="fileToUpload">
</div>
<button type="submit" class="btn btn-primary" name="submit">Upload File</button>
</form>
<div class="progress mt-3">
<div class="progress-bar" role="progressbar" style="width: 0%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
</div>
<div id="uploadStatus" class="mt-3"></div>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
// Check if directory exists, if not create it
if (!is_dir($target_dir)) {
mkdir($target_dir, 0755, true);
}
// Move uploaded file to the target directory
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "<script>document.getElementById('uploadStatus').innerHTML = 'The file " . basename($_FILES["fileToUpload"]["name"]) . " has been uploaded.';</script>";
} else {
echo "<script>document.getElementById('uploadStatus').innerHTML = 'Sorry, there was an error uploading your file.';</script>";
}
}
?>
</div>
<!-- jQuery and Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
$('#uploadForm').on('submit', function(e){
e.preventDefault();
var formData = new FormData(this);
$.ajax({
xhr: function() {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress", function(evt) {
if (evt.lengthComputable) {
var percentComplete = Math.round((evt.loaded / evt.total) * 100);
$('.progress-bar').width(percentComplete + '%');
$('.progress-bar').attr('aria-valuenow', percentComplete);
}
}, false);
return xhr;
},
type: 'POST',
url: '<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>',
data: formData,
contentType: false,
processData: false,
success: function(response) {
$('#uploadStatus').html(response);
}
});
});
});
</script>
</body>
</html>