简体   繁体   中英

PhP Upload only accepting JPEG and PNG, it won't upload JPG

I have a short bit of code to help combat the iOS EXIF issue with JPEG. This works great, it'll also upload PNG files too but it won't upload JPG files. The Session does get set but the image will not upload.

I found out that it will only accept JPEG and JPG on phones, it will not upload either on PC or Laptop.

<?php
session_start();
$target_dir = "uploads/";

$filename = $_FILES['file']['name'];
$filePath = $_FILES['file']['tmp_name'];

$types = array('image/jpeg');

if (in_array($_FILES['file']['type'], $types)) {

$exif = exif_read_data($_FILES['file']['tmp_name']);
if (!empty($exif['Orientation'])) {
$imageResource = imagecreatefromjpeg($filePath); // provided that the                         image is jpeg. Use relevant function otherwise
switch ($exif['Orientation']) {
    case 3:
$image = imagerotate($imageResource, 180, 0);
break;

case 6:
$image = imagerotate($imageResource, -90, 0);
break;

case 8:
$image = imagerotate($imageResource, 90, 0);
break;

default:
$image = $imageResource;
} 
}

imagejpeg($image, 'uploads/'.$filename, 100);
$_SESSION["image"] = $filename;
header("Location: http://perfectprints.org.uk/tool.php");

} else {
if (move_uploaded_file($_FILES['file']['tmp_name'], "uploads/".         $_FILES["file"]['name'])) {
    $_SESSION["image"] = $filename;
    header("Location: http://perfectprints.org.uk/tool.php");
    }
}

?>

This will upload JPEG with the EXIF fixes and PNG files but it won't upload JPG files. How do I fix this issue?

I'm guessing PHP is reading JPG and JPEG as the same file type and run through the EXIF code, maybe that destroys the JPG images?

Thank you.

Some image has not Orientation value in metadata.

If uploaded file has not Orientation info, your $image variable could not generate on your code. You must generated $imageResource for your default case in switch.

Please check your if statement

I'm sorry my bad english :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM