网站制作学习网PHP→正文:PHP上传图片代码
字体:

PHP上传图片代码

PHP 2010/4/12 15:34:32  点击:不统计

原载于:转www.载for网站制作学习asp必.cn究

php上传图片代码,代码如下:
<?$uptypes=array('image/jpg',  //上传文件类型列表
 'image/jpeg',
 'image/png',
 'image/pjpeg',
 'image/gif',
 'image/bmp',
 'image/x-png'); 
$max_file_size=204800;   //上传文件大小限制, 单位BYTE
$destination_folder="fun_photo/"; //上传文件路径
$watermark=1;   //是否附加水印(1为加水印,其他为不加水印);
$watertype=1;   //水印类型(1为文字,2为图片)
$waterposition=1;   //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
$waterstring=""; //水印字符串
$waterimg="";  //水印图片
$imgpreview=0;   //是否生成预览图(1为生成,其他为不生成);
$imgsmallsize=100; //缩略图大小定义
?>
<html>
<head>
<title>上传图片</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{  
if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
//是否存在文件

//echo "<script language='javascript'>alert('上传文件不能为空');</script>";
err_back_message("上传文件不能为空");
 exit;
}

 $file = $_FILES["upfile"];
 if($max_file_size < $file["size"])
 //检查文件大小
 {
//echo "<script language='javascript'>alert('上传文件太大');</script>";
err_back_message("上传文件太大");
 exit;
  }

if(!in_array($file["type"], $uptypes))
//检查文件类型
{
//echo "<script language='javascript'>alert('不支持该类型文件');</script>";
err_back_message("不支持该类型文件");
 exit; 
}
if(!file_exists($destination_folder))
mkdir($destination_folder);
$filename=$file["tmp_name"];
$image_size = getimagesize($filename); 
$pinfo=pathinfo($file["name"]);
$ftype=$pinfo[extension];
//这里可以更改文件上传后的名称,目前采用变量,也可以用时间time()
$destination = $destination_folder.md5($_SESSION['p_id']."fun").".".$ftype;
//if (file_exists($destination) && $overwrite != true)
//{
//echo "<font color='red'>同名文件已经存在了!</a>";
//exit;
//}
if(!move_uploaded_file ($filename, $destination))
 {
   echo "<font color='red'>移动文件出错!</a>";
     exit;
  }
$pinfo=pathinfo($destination); 
 php_cut_resize($destination,$type="1",$bili="0.5",$mwidth="80",$mheight="100",$rename="1",$tname="temp",$clear_type="1");
//这个是剪切图片函数,参考:按比例剪切压缩图片 http://www.forasp.cn/html/1862.html
$fname=$pinfo[basename];
$sql = "update p_info set P_fun_img = '".$destination."' where p_id =".$_SESSION['p_id'];
$num = $db->update($sql);
echo "<script>alert('修改成功!刷新后查看');</script>";
exit;
}
?>
</body>
</html>


原载于:网f站o学a习s制p作.cn

·上一篇:php自动创建文件夹 >>    ·下一篇:php之变量常量 >>
推荐文章
最新文章