How can PHP be used to restrict file uploads to only allow images with the .jpg extension and a maximum size of 250 x 250 pixels?
To restrict file uploads to only allow images with the .jpg extension and a maximum size of 250 x 250 pixels, you can use PHP to check the file extension and dimensions before allowing the upload to proceed. This can be achieved by checking the file extension using the pathinfo() function and verifying the image dimensions using the getimagesize() function.
if ($_FILES['file']['type'] == 'image/jpeg' && $_FILES['file']['size'] <= 250*250) {
$image_info = getimagesize($_FILES['file']['tmp_name']);
if ($image_info[0] <= 250 && $image_info[1] <= 250) {
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
echo 'File uploaded successfully.';
} else {
echo 'Image dimensions must be 250 x 250 pixels or less.';
}
} else {
echo 'Only JPEG images with a maximum size of 250 x 250 pixels are allowed.';
}
Related Questions
- Are there specific configurations in php.ini or apache2.conf that need to be adjusted for certain PHP requests to work?
- What is the potential impact of using filter_input_array with the parameter FILTER_SANITIZE_SPECIAL_CHARS on JSON decoding in PHP?
- How can PHP developers troubleshoot and fix issues related to passing form variables and executing MySQL queries in PHP scripts?