借用别人的一个php单图片上传类,改的一个php多图片上传类

本文介绍了一个用于上传和处理多张图片的PHP类,并详细解释了如何检查文件类型、大小和格式,确保上传的图片符合预设的标准。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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[]">&nbsp;&nbsp;<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 ----------------------------------------------------------------------
...(省略)...  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值