How can PHP be used to restrict file uploads to only images in jpg format?
To restrict file uploads to only images in jpg format using PHP, you can check the file type of the uploaded file before allowing it to be saved on the server. This can be done by checking the MIME type of the file using the `$_FILES` superglobal array.
if ($_FILES['file']['type'] == 'image/jpeg') {
// Process the file upload
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
echo 'File uploaded successfully.';
} else {
echo 'Only JPG images are allowed.';
}
Related Questions
- What best practices should be followed when handling file uploads in PHP to avoid errors like "Das Bild ist nicht im JPG-Format"?
- What are the potential pitfalls of sorting a PHP table using drag and drop functionality?
- How can one optimize database design to avoid having a large number of empty fields in PHP applications?