Dave
Dave

Reputation: 687

IE won't allow users jpg's to upload via PHP - other image extensions work

I'm running an image upload script for users where I have allowed png, jpg, jpeg and gif extensions.

When using IE 7-9, users can only successfully submit png's or gif's. IE users can't seem to get a jpg uploading.

I understand about pjpeg and have modified code accordingly due to this IE issue, however, the issue still occurs. Users on IE cannot upload a jpg but other extensions work just fine.

Any hints? Thank you!

PHP

            $filename = basename($_FILES['photo']['name']);
            $ext = substr($filename, strrpos($filename, '.') + 1);

            //Edit as per comments below
            $ext = strtolower($ext);

            //Check if the file is a JPG, JPEG, GIF or PNG image and it's size is less than 5Mb
            $allowedExts = array('jpg', 'jpeg', 'gif', 'png');
            if ( (in_array($ext, $allowedExts)) &&
            ($_FILES["photo"]["type"] == "image/jpeg") ||
            ($_FILES["photo"]["type"] == "image/png") ||
            //Edit as per comments below
            ($_FILES["photo"]["type"] == "image/x-png") ||
            ($_FILES["photo"]["type"] == "image/gif") ||
            ($_FILES["photo"]["type"] == "image/pjpeg")
            && ($_FILES["photo"]["size"] <= 5242880) ){

            //Name it and determine the path to which we want to save this file
            $newname = str_replace( ' ', '_', trim( strip_tags( $_POST['first-name'].'_'.$_POST['last-name'] ) ) ) . '_' . $formKey->generateKey() . '_' . time() . '.jpg';
            ...  

FORM

<form id="submit-photo" action="index.php?p=uploader" enctype="multipart/form-data" method="POST">
        <?php if(!isset($_SESSION['flash_message']) && isset($_SESSION['recent_field']) ) unset($_SESSION['recent_field']); ?>
        <?php $formKey->outputKey(); ?>
        <input type="hidden" name="MAX_FILE_SIZE" value="5242880" />
                <div id="upload-photo">
                        <input type="file" name="photo" id="photo" />
                </div>
                ...

Upvotes: 3

Views: 643

Answers (1)

Damien Pirsy
Damien Pirsy

Reputation: 25445

I've had similar issues with IE (especially 7 and 8), usually it was due to the "image/x-png" MIME type that IE fires when uploading a png image. Try adding that to your MIME list

$_FILES["photo"]["type"] == "image/x-png"

Consider thought that the MIME type can be spoofed, and it's not a fully reliable assessment of an image. You should use something like getimagesize() to check for a real image, and pathinfo($file, PATHINFO_EXTENSION) (see manual) to get an extension from a file;

Upvotes: 2

Related Questions