IE赢得';t允许用户jpg';s可以通过PHP上传-其他图像扩展也可以


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

我正在为允许png、jpg、jpeg和gif扩展的用户运行图像上传脚本。

使用IE 7-9时,用户只能成功提交png或gif。IE用户似乎无法上传jpg。

我了解pjpeg,由于这个IE问题,我已经相应地修改了代码,然而,问题仍然存在。IE上的用户无法上传jpg,但其他扩展也可以。

有什么提示吗?非常感谢。

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>
                ...

我也遇到过类似的IE问题(尤其是7和8),通常是由于IE在上传png图像时触发的"images/x-png"MIME类型。尝试将其添加到您的MIME列表中

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

考虑一下MIME类型可能是伪造的,并且它不是对图像的完全可靠的评估。您应该使用类似getimagesize()的东西来检查真实图像,使用pathinfo($file, PATHINFO_EXTENSION)(请参阅手册)来从文件中获取扩展名;