How can PHP be used to compare the file size of an uploaded file with a predefined maximum size limit?
To compare the file size of an uploaded file with a predefined maximum size limit in PHP, you can use the $_FILES superglobal array to access the uploaded file's size and compare it with the maximum size limit. If the uploaded file exceeds the maximum size limit, you can display an error message to the user.
$maxFileSize = 5 * 1024 * 1024; // 5MB
if ($_FILES['file']['size'] > $maxFileSize) {
echo "Error: File size exceeds the maximum limit of 5MB.";
} else {
// Proceed with file upload process
}
Related Questions
- What are the best practices for handling mathematical calculations involving multiple operations in PHP?
- Are there any best practices for handling special characters like "&" in XML parsing in PHP to ensure the parser reads the content correctly?
- What are some troubleshooting tips for resolving gdLib-related errors in PHP on a Windows server?