Update php_upload/upload.php
This commit is contained in:
+77
-32
@@ -4,46 +4,91 @@
|
|||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Test Upload</title>
|
<title>Test Upload</title>
|
||||||
|
<!-- Bootstrap CSS -->
|
||||||
|
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h2>Upload a Large File</h2>
|
<div class="container mt-5">
|
||||||
|
<h2>Upload a Large File</h2>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
// Get configuration limits
|
// Get configuration limits
|
||||||
$upload_max_filesize = ini_get('upload_max_filesize');
|
$upload_max_filesize = ini_get('upload_max_filesize');
|
||||||
$post_max_size = ini_get('post_max_size');
|
$post_max_size = ini_get('post_max_size');
|
||||||
$max_execution_time = ini_get('max_execution_time');
|
$max_execution_time = ini_get('max_execution_time');
|
||||||
$memory_limit = ini_get('memory_limit');
|
$memory_limit = ini_get('memory_limit');
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<p><strong>Maximum Upload Size:</strong> <?php echo $upload_max_filesize; ?></p>
|
<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 POST Size:</strong> <?php echo $post_max_size; ?></p>
|
||||||
<p><strong>Maximum Execution Time:</strong> <?php echo $max_execution_time; ?> seconds</p>
|
<p><strong>Maximum Execution Time:</strong> <?php echo $max_execution_time; ?> seconds</p>
|
||||||
<p><strong>Memory Limit:</strong> <?php echo $memory_limit; ?></p>
|
<p><strong>Memory Limit:</strong> <?php echo $memory_limit; ?></p>
|
||||||
|
|
||||||
<form action="test_upload.php" method="post" enctype="multipart/form-data">
|
<form id="uploadForm" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype="multipart/form-data">
|
||||||
Select file to upload:
|
<div class="form-group">
|
||||||
<input type="file" name="fileToUpload" id="fileToUpload">
|
<label for="fileToUpload">Select file to upload:</label>
|
||||||
<input type="submit" value="Upload File" name="submit">
|
<input type="file" class="form-control" name="fileToUpload" id="fileToUpload">
|
||||||
</form>
|
</div>
|
||||||
|
<button type="submit" class="btn btn-primary" name="submit">Upload File</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
<?php
|
<div class="progress mt-3">
|
||||||
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
<div class="progress-bar" role="progressbar" style="width: 0%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
|
||||||
$target_dir = "uploads/";
|
</div>
|
||||||
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
|
|
||||||
|
|
||||||
// Check if directory exists, if not create it
|
<div id="uploadStatus" class="mt-3"></div>
|
||||||
if (!is_dir($target_dir)) {
|
|
||||||
mkdir($target_dir, 0755, true);
|
<?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>
|
||||||
|
|
||||||
// Move uploaded file to the target directory
|
<!-- jQuery and Bootstrap JS -->
|
||||||
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
|
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
|
||||||
echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded.";
|
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
|
||||||
} else {
|
<script>
|
||||||
echo "Sorry, there was an error uploading your file.";
|
$(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>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user