How can a 3MB limit be implemented for image capacity in a PHP file upload form?
To implement a 3MB limit for image capacity in a PHP file upload form, you can check the size of the uploaded file before allowing it to be saved on the server. This can be done by using the $_FILES superglobal array to access the 'size' key, which represents the size of the uploaded file in bytes. You can then compare this size to the desired limit (in this case, 3MB or 3,000,000 bytes) and only proceed with the upload if the file size is within the limit.
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$fileSize = $_FILES['file']['size'];
if ($fileSize <= 3000000) {
// Proceed with the file upload process
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
echo 'File uploaded successfully.';
} else {
echo 'File size exceeds the limit of 3MB.';
}
}