What are some potential pitfalls of using the PHP class for image uploading in the provided code?
One potential pitfall of using the PHP class for image uploading in the provided code is the lack of validation for file type and size, which can lead to security vulnerabilities and potential server overload. To solve this issue, you should add validation checks for file type and size before moving the uploaded file to the desired directory.
// Validate file type and size before moving the uploaded file
if ($_FILES['file']['type'] != 'image/jpeg' && $_FILES['file']['type'] != 'image/png') {
die('Invalid file type. Only JPEG and PNG files are allowed.');
}
if ($_FILES['file']['size'] > 5000000) { // 5MB limit
die('File size is too large. Maximum file size allowed is 5MB.');
}
// Move the uploaded file to the desired directory
if (move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name'])) {
echo 'File uploaded successfully.';
} else {
echo 'Failed to upload file.';
}