How can PHP be used to restrict the size of an uploaded image to 100 KB?
To restrict the size of an uploaded image to 100 KB in PHP, you can check the size of the uploaded file in bytes and compare it to the desired limit before allowing the upload to proceed. If the file exceeds the limit, you can display an error message to the user and prevent the upload.
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$fileSize = $_FILES['image']['size'];
if ($fileSize > 100000) {
echo "Error: File size exceeds the limit of 100 KB.";
} else {
// Proceed with the upload process
}
}
Related Questions
- What are some common debugging strategies for resolving PHP script errors related to function redeclaration?
- What are the potential reasons for a syntax error when using PHP functions like preg_match or file_get_contents?
- What are some best practices for validating variable contents in PHP to ensure they only contain specific characters?