Create some javascript that limits the amount of files to 20 that can be uploaded at once.

This commit is contained in:
johnnyq 2023-08-23 16:48:42 -04:00
parent 811f253470
commit 1083ac88d9
2 changed files with 29 additions and 3 deletions

View File

@ -12,7 +12,7 @@
<div class="modal-body bg-white">
<div class="form-group">
<input type="file" class="form-control-file" name="file[]" multiple accept=".jpg, .jpeg, .gif, .png, .webp, .pdf, .txt, .md, .doc, .docx, .odt, .csv, .xls, .xlsx, .ods, .pptx, .odp, .zip, .tar, .gz, .xml, .msg, .json, .wav, .mp3, .ogg, .mov, .mp4, .av1">
<input type="file" class="form-control-file" name="file[]" multiple id="fileInput" accept=".jpg, .jpeg, .gif, .png, .webp, .pdf, .txt, .md, .doc, .docx, .odt, .csv, .xls, .xlsx, .ods, .pptx, .odp, .zip, .tar, .gz, .xml, .msg, .json, .wav, .mp3, .ogg, .mov, .mp4, .av1">
</div>
<small class="text-secondary">Multiple files can be uploaded by holding down CTRL and selecting files</small>
@ -25,3 +25,29 @@
</div>
</div>
</div>
<script>
const maxFiles = 20; // Set the maximum number of allowed files
const fileInput = document.getElementById('fileInput');
const uploadForm = document.getElementById('uploadForm');
fileInput.addEventListener('change', function () {
if (fileInput.files.length > maxFiles) {
alert(`You can only upload up to ${maxFiles} files at a time.`);
resetFileInput();
}
});
uploadForm.addEventListener('submit', function (event) {
if (fileInput.files.length > maxFiles) {
event.preventDefault();
alert(`You can only upload up to ${maxFiles} files at a time.`);
resetFileInput();
}
});
function resetFileInput() {
fileInput.value = ''; // Clear the selected files
}
</script>

View File

@ -35,14 +35,14 @@ if (isset($_POST['add_files'])) {
move_uploaded_file($file_tmp_path, $dest_path);
// Extract .ext from reference file name to be used to store SHA256 hash value
// Extract .ext from reference file name to be used to store SHA256 hash
$file_hash = strstr($file_reference_name, '.', true) ?: $file_reference_name;
mysqli_query($mysqli,"INSERT INTO files SET file_reference_name = '$file_reference_name', file_name = '$file_name', file_ext = '$file_extension', file_hash = '$file_hash', file_client_id = $client_id");
//Logging
$file_id = intval(mysqli_insert_id($mysqli));
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'File', log_action = 'Upload', log_description = '$file_name', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $file_id");
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'File', log_action = 'Upload', log_description = '$session_name uploaded $file_name', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $file_id");
} else {
$_SESSION['alert_message'] = 'There was an error moving the file to upload directory. Please make sure the upload directory is writable by web server.';
}