在做upload的时候
比如上传一个gif的图片
用$_FILES['file']['tmp_name']获取mime的值 为什么不等于 IMAGETYPE_GIF帮我看看吧,下面是代码<?php
$db=mysql_connect('localhost','root','xiaozhuo') or die ('Funk you!');
mysql_select_db('moviesite',$db) or die(mysql_error($db));$dir='c:/Progrm Files/Apache Software Foundation/Apache2.2/htdocs/images';if($_FILES['uploadfile']['error']!=UPLOAD_ERR_OK)
{
switch ($_FILES['uploadfile']['error']){
case UPLOAD_ERR_INI_SIZE:
die('The uploaded file exceeds the upload_max_filesize directvie'.'in php.ini.');
break;
case UPLOAD_ERR_FORM_SIZE:
die('The uploaded file exceeds the MAX_FILE_SIZE directive that'.'was specofied in the HTML form.');
break;
case UPLOAD_ERR_PARTIAL:
die('The uploaded file was only partially uploaded.');
break;
case UPLOAD_ERR_NO_FILE:
die('No file was uploaded.');
break;
case UPLOAD_ERR_NO_TMP_DIR:
die('The server is missing a temporary folder.');
break;
case UPLOAD_ERR_CANT_WRITE:
die('The server failed to write the uploaded file to disk.');
break;
case UPLOAD_ERR_EXTENSION:
die('File upload stopped by extension.');
break;
}
}
$image_caption=$_POST['caption'];
$image_username=$_POST['usernama'];
$image_date=@date('y-m-d');
list($width,$height,$type,$attr)=getimagesize($_FILES['uploadfile']['tmp_name']);
$zhang=getimagesize($_FILES['uploadfile']['tmp_name']);
echo $zhang['mime'];switch($zhang['mime']){
case 'image/gif':
$image=imagecreatefromgif($_FILES['uploadfile']['tmp_name'])or die ('The file you uploaded was not a supported filetype.');
$ext='.gif';
break;
case IMAGETYPE_JPEG:
$image=imagecreatefromjpeg($_FILES['uploadfile']['tmp_name'])or die ('The file you uploaded was not a supported filetype.');
$ext='.jpg';
break;
case IMAGETYPE_PNG:
$image=imagecreatefrompng($_FILES['uploadfile']['tmp_name'])or die ('The file you uploaded was not a supported filetype.');
$ext='.png';
break;
default:
die('The file you uploaded was not a supported filetype.');
}$query='INSERT INTO images
(image_caption,image_username,image_date)
VALUES
("'.$image_caption.'","'.$image_username.'","'.$image_date.'")';
$result=mysql_query($query,$db) or die (mysql_error());$last_id=mysql_insert_id();$imagename=$last_id.$ext;
$query='UPDATE images
SET image_filename="'.$imagename.'"
WHERE $image_id ='.$last_id;
$result=mysql_query($query,$db)or die(mysql_error($db));switch($type){
case IMAGETYPE_GIF:
$image=imagegif($dir.'/'.$imagename);
break;
case IMAGETYPE_JPEG:
$image=imagejpeg($dir.'/'.$imagename);
break;
case IMAGETYPE_PNG:
$image=imagepng($dir.'/'.$imagename);
break;
}
imagedestroy($image);?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Here is your pic!</title>
</head><body>
<h1>So how does it feel to be famous?</h1>
<p>Here is the picture you just uploaded to our servers:</p>
<img src=image/"<?php echo $imagename; ?>" style="float:left;" />
<table>
<tr><td>Image Saved as:</td><td><?php echo $imagename; ?></td></tr>
    <tr><td>Image Saved as:</td><td><?php echo $ext;?></td></tr>
    <tr><td>Height:</td><td><?php echo $height; ?></td></tr>
    <tr><td>Width:</td><td><?php echo $width; ?></td></tr>
    <tr><td>Upload Date:</td><td><?php echo $image_date; ?></td></tr>
    <tr><td><?php print_r(getimagesize($_FILES['uploadfile']['tmp_name'])); ?></td></tr>
</table>
</body>
</html>

解决方案 »

  1.   

    $_FILES['file']['tmp_name'] 上传文件的临时路径
    $_FILES['file']['type']
      

  2.   

    手册上说:
    $_FILES['userfile']['type']
    文件的 MIME 类型,如果浏览器提供此信息的话。一个例子是“image/gif”。不过此 MIME 类型在 PHP 端并不检查,因此不要想当然认为有这个值。 以下是我说:
    所以不建议使用这个方法获取文件类型,我测试过,就是相同的文件用IE和FF上传的时候获得的MIME值也是不一样的。而且好像浏览器提供这个信息的时候只看文件扩展名,并不检测其真正格式,比如把一个txt文件直接改名叫xxx.gif,上传后获得的值就成了image/gif了。
      

  3.   

    楼主既然已经用getimagesize获取了图片信息,而又想用IMAGETYPE常量就应该用返回值的索引2($zhang[2])比较。手册上说:索引 2 是图像类型的标记:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM。这些标记与 PHP 4.3.0 新加的 IMAGETYPE 常量对应
    ----------------------------------------
    list($width,$height,$type,$attr)=getimagesize($_FILES['uploadfile']['tmp_name']);
    $zhang=getimagesize($_FILES['uploadfile']['tmp_name']); 
     
    $type == $zhang[2] == IMAGETYPE_GIF == 1 <> $zhang['mime']