What are the potential pitfalls of allowing users to upload images without size restrictions in PHP?
Allowing users to upload images without size restrictions can lead to potential security vulnerabilities such as denial of service attacks, server resource exhaustion, and storage space issues. To mitigate these risks, it is essential to enforce size restrictions on uploaded images to prevent malicious users from uploading excessively large files.
// Set a maximum file size limit for uploaded images
$maxFileSize = 5 * 1024 * 1024; // 5 MB
if ($_FILES['image']['size'] > $maxFileSize) {
// Handle error: File size exceeds the limit
echo "Error: File size exceeds the limit of 5 MB.";
} else {
// Process the uploaded image
move_uploaded_file($_FILES['image']['tmp_name'], 'uploads/' . $_FILES['image']['name']);
echo "Image uploaded successfully.";
}