How can the file size for upload be determined on the client side in PHP?
When uploading files in PHP, it is important to limit the file size to prevent large files from overwhelming the server. To determine the file size on the client side before uploading, you can use JavaScript to check the file size and then pass that information to the server-side PHP script.
// HTML form with JavaScript to check file size before submitting
<form action="upload.php" method="post" enctype="multipart/form-data" onsubmit="return checkFileSize()">
<input type="file" name="file" id="file">
<input type="submit" value="Upload File">
</form>
<script>
function checkFileSize() {
var fileInput = document.getElementById('file');
if (fileInput.files[0].size > 5242880) { // 5MB limit
alert('File size exceeds the limit of 5MB.');
return false;
}
return true;
}
</script>