gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:13:06 26 4
gpt4 key购买 nike

我正在为允许 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com