uploadPic.php(多图片上传类)
------------------------------------------------------------------------
<?
class uploadPic
{
var $imgtype;
var $mimetype;
var $path;
var $error;
var $picname;
var $picwidth;
var $picheight;
//返回函数
function goback($str)
{
echo "<script>alert('".$str."');history.go(-1);</script>";
exit();
}
function check($file,$i)//检查文件类型
{
if($_FILES[$file]["name"][$i] == "")
$this->goback("没有文件要上传");
$types = array("image/gif","image/jpeg","image/pjpeg","image/png");
if(!in_array($_FILES[$file]["type"][$i],$types))
$this->goback("请用jpg/gif/png格式的图片");
if($_FILES[$file]["size"][$i] > 300000)
$this->goback('对不起,图片大小不能超过300k,请用photoshop压缩或换一幅小的图片!');
$this->mimetype = $_FILES[$file]["type"][$i];
$size = getimagesize($_FILES[$file]['tmp_name'][$i]);
$this->picwidth = $size[0];
$this->height = $size[1];
$this->path = $_FILES[$file]["tmp_name"][$i];
$this->error = $_FILES[$file]["error"][$i];
$this->getType();
}
function getType()//返回文件扩展名
{
switch($this->mimetype)
{
case "image/gif":
$this->imgtype = ".gif";
break;
case "image/jpeg":
$this->imgtype = ".jpg";
break;
case "image/pjpeg":
$this->imgtype = ".jpg";
break;
case "image/png":
$this->imgtype = ".png";
break;
}
}
function getError()
{
switch($this->error)
{
case 0:
break;
case 1:
$this->goback("文件大小超过限制,请缩小后再上传");
break;
case 2:
$this->goback("文件大小超过限制,请缩小后再上传");
break;
case 3:
$this->goback("文件上传过程中出错,请稍后再上传");
break;
case 4:
$this->goback("文件上传失败,请重新上传");
break;
}
}
function upfile($path,$file,$i,$pro_id)
{
$this->check($file,$i);
//$this->goback($this->mimetype);
//$this->picname = date('YmdHis').rand(0,9);
$this->picname = "product".$pro_id."_".$i;
$this->getError();
move_uploaded_file($this->path,$path.$this->picname.$this->imgtype);
$this->picname = $this->picname.$this->imgtype;
}
}
?>
form.htm (表单页)
------------------------------------------------------------------------------
<!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=gb2312">
<title>新增产品</title>
<script language="javascript" type="text/javascript" src="js/addFile.js" charset="UTF-8"></script>
</head>
<body onkeydown="keyDown()">
<form id="form1" name="form1" method="post" action="do.php" enctype="multipart/form-data"">
<div align="center">
<input type="file" name="file[]" id="file[]" onchange="c(this)">
<div id="fileDiv" align="left" style="vertical-align:top"></div>
<a href="javascript:addFile();">添加图片</a> ( <span>至少上传一幅图片,图片文件大小必须 < 100 K</span> )
</div>
</form>
</body>
</html>
addFile.js (点一下增加一个文件上传框的js)
----------------------------------------------------------------------------
<!--
var boxcount=0;
function addFile() {
if(boxcount<4){
var fileDiv = document.all['fileDiv'];
var strHtml = '<span><input type="file" name="file[]" id="file[]"> <button onclick="removeFile(parentNode);">删除</button><br></span>';
fileDiv.innerHTML += strHtml + '';
boxcount++;
}else
{
alert("You can't add more than 5 pictures for one part.")
}
}
function removeFile(obj) {
boxcount--;
obj.removeNode(true);
}
-->
do.php (上传图片)
------------------------------------------------------------------------------
...(省略)...
//上传图片 - start ----------------------------------------------------------------------
require_once("../include/uploadPic.php");
if(!is_dir("../product/bimages")){mkdir("../product/bimages");}
for($i=0;$i<count($_FILES["file"]);$i++){
if(!$_FILES["file"]['name'][$i]==''){
$pic = new uploadPic;
$pic->upfile("../product/bimages/",'file',$i,$affected_id);//第一个参数是要保存在服务器上的路径,第二个参数是上传控件的名字,第三个参数是数组下标,第四个参数是产品id(以便修改图片名字为:产品id_0.xxx/产品id_1.xxx/产品id_0.xxx 的形式)
$compicname = $pic->picname;//上传后的图片名字
$arrpicname .= $compicname."|";//多图以|隔开
}
}
//上传图片 - end ----------------------------------------------------------------------
...(省略)...
------------------------------------------------------------------------
<?
class uploadPic
{
var $imgtype;
var $mimetype;
var $path;
var $error;
var $picname;
var $picwidth;
var $picheight;
//返回函数
function goback($str)
{
echo "<script>alert('".$str."');history.go(-1);</script>";
exit();
}
function check($file,$i)//检查文件类型
{
if($_FILES[$file]["name"][$i] == "")
$this->goback("没有文件要上传");
$types = array("image/gif","image/jpeg","image/pjpeg","image/png");
if(!in_array($_FILES[$file]["type"][$i],$types))
$this->goback("请用jpg/gif/png格式的图片");
if($_FILES[$file]["size"][$i] > 300000)
$this->goback('对不起,图片大小不能超过300k,请用photoshop压缩或换一幅小的图片!');
$this->mimetype = $_FILES[$file]["type"][$i];
$size = getimagesize($_FILES[$file]['tmp_name'][$i]);
$this->picwidth = $size[0];
$this->height = $size[1];
$this->path = $_FILES[$file]["tmp_name"][$i];
$this->error = $_FILES[$file]["error"][$i];
$this->getType();
}
function getType()//返回文件扩展名
{
switch($this->mimetype)
{
case "image/gif":
$this->imgtype = ".gif";
break;
case "image/jpeg":
$this->imgtype = ".jpg";
break;
case "image/pjpeg":
$this->imgtype = ".jpg";
break;
case "image/png":
$this->imgtype = ".png";
break;
}
}
function getError()
{
switch($this->error)
{
case 0:
break;
case 1:
$this->goback("文件大小超过限制,请缩小后再上传");
break;
case 2:
$this->goback("文件大小超过限制,请缩小后再上传");
break;
case 3:
$this->goback("文件上传过程中出错,请稍后再上传");
break;
case 4:
$this->goback("文件上传失败,请重新上传");
break;
}
}
function upfile($path,$file,$i,$pro_id)
{
$this->check($file,$i);
//$this->goback($this->mimetype);
//$this->picname = date('YmdHis').rand(0,9);
$this->picname = "product".$pro_id."_".$i;
$this->getError();
move_uploaded_file($this->path,$path.$this->picname.$this->imgtype);
$this->picname = $this->picname.$this->imgtype;
}
}
?>
form.htm (表单页)
------------------------------------------------------------------------------
<!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=gb2312">
<title>新增产品</title>
<script language="javascript" type="text/javascript" src="js/addFile.js" charset="UTF-8"></script>
</head>
<body onkeydown="keyDown()">
<form id="form1" name="form1" method="post" action="do.php" enctype="multipart/form-data"">
<div align="center">
<input type="file" name="file[]" id="file[]" onchange="c(this)">
<div id="fileDiv" align="left" style="vertical-align:top"></div>
<a href="javascript:addFile();">添加图片</a> ( <span>至少上传一幅图片,图片文件大小必须 < 100 K</span> )
</div>
</form>
</body>
</html>
addFile.js (点一下增加一个文件上传框的js)
----------------------------------------------------------------------------
<!--
var boxcount=0;
function addFile() {
if(boxcount<4){
var fileDiv = document.all['fileDiv'];
var strHtml = '<span><input type="file" name="file[]" id="file[]"> <button onclick="removeFile(parentNode);">删除</button><br></span>';
fileDiv.innerHTML += strHtml + '';
boxcount++;
}else
{
alert("You can't add more than 5 pictures for one part.")
}
}
function removeFile(obj) {
boxcount--;
obj.removeNode(true);
}
-->
do.php (上传图片)
------------------------------------------------------------------------------
...(省略)...
//上传图片 - start ----------------------------------------------------------------------
require_once("../include/uploadPic.php");
if(!is_dir("../product/bimages")){mkdir("../product/bimages");}
for($i=0;$i<count($_FILES["file"]);$i++){
if(!$_FILES["file"]['name'][$i]==''){
$pic = new uploadPic;
$pic->upfile("../product/bimages/",'file',$i,$affected_id);//第一个参数是要保存在服务器上的路径,第二个参数是上传控件的名字,第三个参数是数组下标,第四个参数是产品id(以便修改图片名字为:产品id_0.xxx/产品id_1.xxx/产品id_0.xxx 的形式)
$compicname = $pic->picname;//上传后的图片名字
$arrpicname .= $compicname."|";//多图以|隔开
}
}
//上传图片 - end ----------------------------------------------------------------------
...(省略)...