【发布时间】:2012-02-23 03:35:52
【问题描述】:
我下面的脚本是供用户将个人资料图片上传到他们的帐户。如果他们想上传新的个人资料图片,它也会删除当前的个人资料图片。
过去几个月我一直在努力解决的问题是构建一个在所有浏览器上始终有效的图像上传表单。关于如何使我的上传脚本更健壮,我有更广泛的问题,但特别是我遇到了一个主要问题。
为什么这个脚本不能与 Internet Explorer 一起使用?通过 Chrome、Firefox 和 Safari 上传都可以正常工作。但是,当我使用 IE 上传图像时,它会因为文件类型不正确而被拒绝。
这是我在尝试解决此问题的过程中尝试/研究的内容。
1) 确保 enctype="multipart/form-data" 包含在我的 html 表单中
2) 尝试使用exif_imagetype($file) 来获取文件类型,而不是getimagesize()(注意:我还在我的php.ini 文件中启用了exif_imagetype())。我读到这是一种更可靠的确定文件类型的方法。该功能在其他三个浏览器上都可以使用,但是在使用 IE 时仍然无法确定文件类型。
3) 我使用var_dump($_FILES) 查看正在上传的内容。这会在除 IE 之外的所有浏览器上显示名称、大小、类型等。在 IE 上似乎没有上传文件的名称。呼应名称显示:"string '' (length=0)"
html 表单、图片上传脚本和图片调整大小脚本都位于下方。
表格:
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="625000">
<input type="file" class="span2" name="image" id="image" size="20">
<input name="picture" type="submit" value="update" class="btn btn-primary" style="margin-bottom: -10px">
</form>
上传脚本:
if (isset($_POST['picture'])){
//THIS SECTION IS FOR UPLOADING THE PROFILE PICTURE.
//It will create a standard profile image size 320 by 320.
//It also then creates a 'thumbnail' picture size 100 x 100
//delete the original profile image (if there was one)
$CurrentImage = getImage("profile",$id,FALSE);
$CurrentImageThumb = getImage("profile",$id,TRUE);
//if the current image is something other than the generic image, delete the current images
if($CurrentImage!="images/profile/generic.jpg")unlink($CurrentImage); if($CurrentImageThumb!="images/profile/genericthumb.jpg")unlink($CurrentImageThumb);
//get the type of the upload
$pic_type = ".".pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
//save the initial file
$saveto = "images/profile/$id"."$pic_type";
move_uploaded_file($_FILES['image']['tmp_name'], $saveto);
//resize it and display the appropriate message if it works/doesn't work
//CREATE FULL SIZE
if (true === ($pic_error = image_resize($saveto,$saveto, 320, 320, 0))){
//CREATE THUMBNAIL
$saveto2 = "images/profile/$id"."thumb"."$pic_type";
if (true === ($pic_error = image_resize($saveto,$saveto2, 100, 100, 0))){
showAlert(2,"Your image has been uploaded!"," If the image did not change, <a href=\"./editprofile\">click here</a> to reload the page.");
}}else{
showAlert(3,"File Issue: ",$pic_error);
unlink($saveto);//delete the upload
unlink($saveto2);//delete the upload
}
}
图像调整脚本:
function image_resize($src, $dst, $width, $height, $crop=0){
//if getimagesize doesn't output an array with the first two values being width and height, we reject the file
if(!list($w, $h) = getimagesize($src)) return "Unsupported file type. We only accept image files the extension .jpg, .jpeg, .png, .gif, or .bmp";
//get the image type based on the file extension
$type = strtolower(substr(strrchr($src,"."),1));
//based on file type, create a new image from the url
if($type == 'jpeg') $type = 'jpg';
switch($type){
case 'bmp': $img = imagecreatefromwbmp($src); break;
case 'gif': $img = imagecreatefromgif($src); break;
case 'jpg': $img = imagecreatefromjpeg($src); break;
case 'png': $img = imagecreatefrompng($src); break;
default : return "Unsupported file type. We only accept image files with the extension .jpg, .jpeg, .png, .gif, or .bmp";
}
// resize (get the dimensions for resize if $crop or not)
if($crop){
//if initial image is smaller than crop size display error
if($w < $width or $h < $height) return "Picture is too small. Your image must be LARGER than ".$width."pixels by ".$height." pixels.";
$ratio = max($width/$w, $height/$h);
$h = $height / $ratio;
$x = ($w - $width / $ratio) / 2;
$w = $width / $ratio;
}
else{
if($w < $width and $h < $height) return "Picture is too small. Your image must be LARGER than ".$width."pixels by ".$height." pixels.";
$ratio = min($width/$w, $height/$h);
$width = $w * $ratio;
$height = $h * $ratio;
$x = 0;
}
//create a new true color image
$new = imagecreatetruecolor($width, $height);
// preserve transparency
if($type == "gif" or $type == "png"){
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
imagealphablending($new, false);
imagesavealpha($new, true);
}
//COPY AND RESIZE THE IMAGE
/**imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y ,
int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
imagecopyresampled() will take an rectangular area from src_image of width src_w and height src_h at
position (src_x,src_y) and place it in a rectangular area of dst_image of width dst_w and height dst_h at position (dst_x,dst_y).
*/
imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h);
//then output the image to the file
switch($type){
case 'bmp': imagewbmp($new, $dst); break;
case 'gif': imagegif($new, $dst); break;
case 'jpg': imagejpeg($new, $dst); break;
case 'png': imagepng($new, $dst); break;
}
return true;
}
【问题讨论】:
-
要回答“为什么某些东西不起作用”的问题,必须调试代码
-
我已在本地站点上启用错误报告。出现的唯一错误是:getimagesize(images/profile/151.) [function.getimagesize]: failed to open stream: No such file or directory in C:\www\local\php\image.php on line 25
-
关于如何更彻底地调试的任何建议?
-
从错误消息中可以明显看出您的扩展名是空的。奇怪的是 move_uploaded_file 没有引发任何错误。首先检查 $_FILES['image']['error']
-
谢谢。抛出的文件错误是#4,它表示没有上传文件。这对我来说没有意义,因为如果我选择使用 Chrome 上传的文件,上传就可以了。如果我在尝试上传完全相同的文件时使用 IE,则会引发“找不到文件”错误。有什么想法吗?