php - IE 不允许用户通过 PHP 上传 jpg - 其他图像扩展有效

标签 php file-upload image-uploading

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

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

由于这个 IE 问题,我了解 pjpeg 并相应地修改了代码,但是,问题仍然存在。 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 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 图像时触发的“image/x-png”MIME 类型。 尝试将其添加到您的 MIME 列表中

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

考虑到 MIME 类型可以被欺骗,并且它不是对图像的完全可靠的评估。你应该使用类似 getimagesize() 的东西检查真实图像,pathinfo($file, PATHINFO_EXTENSION)(参见 manual)从文件中获取扩展名;

关于php - IE 不允许用户通过 PHP 上传 jpg - 其他图像扩展有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14856724/

相关文章:

php - 如何在php中使用soap类(示例)?

php - 从数据库中检索行总和

javascript - 上传取消方法未触发

android - 在 Android 中发布大文件

jquery - 使用 jQuery.ajax 发送 multipart/formdata

javascript - 获取json编码形式的信息

php - 内部连接错误

ios - 使用 alamofire 上传多个图像,而不使用 swift 中的 for 循环

Java Eclipse 将图像上传到包内的图像文件夹

java - 如何从谷歌应用引擎(java)将图像检索到jsp中