【发布时间】:2013-01-29 04:28:20
【问题描述】:
我正在为允许 png、jpg、jpeg 和 gif 扩展名的用户运行图像上传脚本。
使用 IE 7-9 时,用户只能成功提交 png's 或 gif's。 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>
...
【问题讨论】:
-
你有没有做过
echo $_FILES["photo"]["type"]看看你在使用IE时得到了什么价值? -
. _ .非常适合在文档中表达感受,但对于实际代码而言则不然。 -
除此之外,我会使用pathinfo 来确定文件名或扩展名。使用创意文件名应该比拥有 substr-functions 更健壮。
-
尝试设置 $ext = strtolower($ext);在你做 substr... Mayde 你的扩展都是像 JPG 这样的大写字母,因为 php 区分大小写,这可能是潜在的问题。
-
. _ .应该是. '_' .在$newname行上。
标签: php file-upload image-uploading