正好刚写了简单的上传图片小程序,我这么判断jpeg: function check_jpeg($f) { //JPEG image should start with 0xFFD8 and end with 0xFFD9 if($fp = fopen($f,"r+b")) { if(fread($fp,2)==chr(255).chr(216)) { /* //not every JPEG image has an standard end of file(EOF):FF D9. fseek($fp,-2); if(fread($fp,2)==chr(255).chr(217)) { //it's a JPEG image fclose($fp); return true; } else { return false; } */ fclose($fp); return true; } else { return false; } } //file not opened return false; } 至于gif就好办多了: $imginfo = getimagesize($f); if($imginfo[2] == 1) return "gif";需要说明的是用getimagesize()判断jpg是不行的,起码在win32上不行,因为无论是jpg或是其他的什么格式,这个$imginfo[2]的值都是2。
@mkdir($uploaddir);//创建路径
$imgtype = $_FILES['product_image']['type']; //类型
$imgsize = $_FILES['product_image']['size']; //大小
$imgname = ""; //名称
$imgmaxsize ="1000000";
$imgwidth = "300";
$imgheight ="300";
if($imgsize>$imgmaxsize)
{
$error_message = "您上传的文件太大,请不要超过1M!";
echo("<script>");
echo("alert('$error_message');");
echo("</script>");
exit;
}
if(!$error_message)
{
$imgsize = GetImageSize($product_image);
if($imgsize[0]>$imgwidth||$imsize[1]>$imgheight)
{
$error_message="您上传的图片尺寸过大,为了不影响发布效果,请不要超过300*300";
echo("<script>");
echo("alert('$error_message');");
echo("</script>");
exit;
}
}
if (!$error_message)
{
if($imgtype == "image/pjpeg"){
$extension=".jpg";
}elseif($imgtype == "image/gif"){
$extension=".gif";
}elseif($imgtype==""){$error_message="";
}else{
$error_message="Please upload images with the extension .jpg or .jpeg or .gif or .png only";
echo("<script>");
echo("alert('$error_message');");
echo("</script>");
exit;
} //错误处理结束 $up_imagename = "img".date("YmdHis").$imgname.$extension;//定义上传之后的文件
if(!$error_message) //上传文件
{
copy($product_image,$uploadpath.$up_imagename); //将文件从服务器临时目录拷贝到目的路径
unlink($product_image); //删除服务器上的临时文件
}
}
$upload_image_path= "/upload/images/".$up_imagename;
}
//添加到数据库
if($pro_id=="") $pro_id="0";
if($pro_type=="") $pro_type=$product_name;
$insert_sql = "insert into product_new (NAME,TYPE,INTRODUCE,PARENT,IMAGE) VALUES('";
$insert_sql.= $product_name."','".$pro_type."','".$product_introduce."','".$pro_id."','".$upload_image_path."')";
echo $insert_sql;
$insert_query = $db->dbQuery($insert_sql);
if( $insert_query)
{
echo("<script language=javascript>\n");
//确定继续添加还是返回主页面
echo("var returnval = window.confirm('添加成功,按-确定继续添加,按-取消返回主页面。')\n");
echo("if(returnval)\n");
echo(" window.location.href='product_add.php?mode=add'\n");
echo("else\n");
echo(" window.location.href='add_product_tog.php'\n");
echo("</script>\n");
exit ;
}
echo mysql_error()."更新失败,请重新<a href=\"product_add.php?mode=add\">尝试</a>\n";
exit ;
如gif,swf等,
更不能用截取文件名的方式,随便改个名都会出错~
function check_jpeg($f)
{
//JPEG image should start with 0xFFD8 and end with 0xFFD9 if($fp = fopen($f,"r+b"))
{
if(fread($fp,2)==chr(255).chr(216))
{
/*
//not every JPEG image has an standard end of file(EOF):FF D9.
fseek($fp,-2); if(fread($fp,2)==chr(255).chr(217))
{
//it's a JPEG image
fclose($fp);
return true;
}
else
{
return false;
}
*/ fclose($fp);
return true;
}
else
{
return false;
}
} //file not opened
return false;
}
至于gif就好办多了:
$imginfo = getimagesize($f);
if($imginfo[2] == 1) return "gif";需要说明的是用getimagesize()判断jpg是不行的,起码在win32上不行,因为无论是jpg或是其他的什么格式,这个$imginfo[2]的值都是2。