PHP在线批量上传图片生成图库或相册程序,注意,本程序是结合数据库实现的,因此在使用前请更改connect.php中连接数据的信息。在你的数据库中创建两个数据表:创建图片表 pre_pic,创建相册表pre_album,以上信息修改后在PHP服务器下运行addpic.php.用户浏览选择图片后,批量上传,要输入相册名称和自定义相册编号,有需要PHP上传图片功能的,本源码比较适合参考学习。
15070.jpg

一、本项目共有三个文件,分别为:AddPic.php(批量上传图片)、AddPicprocess.php(上传图片处理)、connect.php(数据库连接)

二、具体功能及代码如下:

1、AddPic.php(批量上传图片)


<!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" />
<link rel="stylesheet" href="../css/common.css" type="text/css" />
<title>上传图片</title>
</head>
<body>
<div id="man_zone">
<p>选择图片</p>
选择文件 图片描述    
   
   
   
   
   
   
 
选择相册
相册编号
相册名称
 


####1、AddPicprocess.php(上传图片处理)
```php
<?php 
        header("Content-type: text/html;charset=utf-8");

    require 'connect.php';
$conn=new SqlTool();

$albumid=$_POST['albumid'];//相册编号
$albumname=$_POST['albumname'];//相册名称
$depict=$_POST['depict'];
$dateline=date("Y-m-d H:i:m");
    $dest_folder   =  "picture/";   
    $upcount=0;
    if(!file_exists($dest_folder))
    {      
       if(mkdir($dest_folder))
        {

       }//end of if(mkdir($dest_folder))
          else
        {
            echo"创建文件夹失败<br/>";
        }
    }  //end of if(!file_exists($dest_folder))
    foreach ($_FILES["pictures"]["error"] as $key => $error ) 
    {       
        $i=0;
        if ($error == UPLOAD_ERR_OK) 
        {        
            $tmp_name = $_FILES["pictures"]["tmp_name"][$key];     
            $name    = $_FILES["pictures"]["name"][$key];        
            $uploadfile = $dest_folder.$name; 
            $filepath="../nongxiaotushu/Manage/pic/";
            $filepath=$filepath.$uploadfile;        
            move_uploaded_file($tmp_name, $uploadfile);  
            $sql="INSERT INTO `nongxiaotushu3`.`pre_pic` (`albumid`, `dateline`,`depict`,`filepath` ) VALUES ( '$albumid','$dateline', '$depict[$i]','$filepath')";
                        //nongxiaotushu3为数据名称
              $b=$conn->execute_dml($sql);

                if($b)
                {
                    //echo"成功";
                    $i++;
                    $upcount++;
                    //header("location:login.php?errno=2");
                }
                else
                {
                    echo"失败";
                    die("失败".mysql_error());
                }
            }//end of if
      } //end of foreach 

        $sql="select `picnum` from `pre_album` where `albumid`='$albumid'";

        $res=$conn->execute_dql($sql);

        if($row=mysql_fetch_row($res))
        {
                $piccount=$row[0];

                $piccount=$piccount+$upcount;

                $sql="UPDATE `nongxiaotushu3`.`pre_album` SET  `picnum`='$piccount' where `pre_album`.`albumid`='$albumid'";

                echo"修改相册数量"."$sql<br/>";

                $b=$conn->execute_dml($sql);

                        if($b)
                        {
                        //  //echo"相册修改成功";
                            //header("PicManage.php");
                        }
                        else
                        {
                            die("相册修改失败".mysql_error());
                        }
        }

        else
        {
            $sql="INSERT INTO `nongxiaotushu3`.`pre_album` (`albumid`,`albumname`, `dateline`,`picnum`,`pic` ) VALUES ( '$albumid','$albumname','$dateline', $upcount,'$filepath')";
            echo "相册添加"."$sql<br/>";
                  $b=$conn->execute_dml($sql);

                    if($b)
                    {
                        echo"相册添加成功";
                        echo"$sql<br/>";

                        //header("location:login.php?errno=2");
                    }
                    else
                    {
                    //  echo"相册添加失败";
                        die("失败".mysql_error());
                    }

        }

?>   

3、connect.php(数据库连接文件)

<?php

    //固定长度字符串
    function fixstr($ostr,$length){

        $num_str = $ostr;
        $num_strlength = count($num_str);
        if ($length > $num_strlength) {
            $num_str=str_pad($num_str,$length,".",STR_PAD_RIGHT);
        }
        return $num_str;
    }
    class SqlTool {

        //属性
        private $conn;
        private $host="localhost";//数据库地址
        private $user="root";//数据库登陆用户名
        private $password="root";//数据库登陆密码
        private $db="nongxiaotushu3";//数据库名称

        function SqlTool(){

            $this->conn=mysql_connect($this->host,$this->user,$this->password);
            if(!$this->conn){
                die("连接数据库失败".mysql_error());
            }
            mysql_select_db($this->db,$this->conn);
            mysql_query("set names utf8");
        }

        //方法..

        // 完成select dql
        public  function execute_dql($sql){

            $res=mysql_query($sql,$this->conn) or die(mysql_error());

            return $res;

        }
        //完成 update,delete ,insert dml
        public  function execute_dml($sql){

            $b=mysql_query($sql,$this->conn);
            //echo "添加的id=".mysql_insert_id($this->conn);
            if(!$b){
                return 0;//失败
            }else{
                if(mysql_affected_rows($this->conn)>0){
                    return 1;//表示成功
                }else{
                    return 2;//表示没有行数影响.
                }
            }
        }
    }

?>

用到的两个数据库表

1、创建图片表 pre_pic
CREATE TABLE IF NOT EXISTS `pre_pic` (
  `picid` int(8) NOT NULL AUTO_INCREMENT,
  `albumid` mediumint(8) NOT NULL,
  `dateline` datetime NOT NULL,
  `depict` varchar(255) NOT NULL,
  `filepath` varchar(255) NOT NULL,
  PRIMARY KEY (`picid`)
) ENGINE=MyISAM  DEFAULT CHARSET=gbk AUTO_INCREMENT=177 ;

2、创建相册表pre_album

CREATE TABLE IF NOT EXISTS `pre_album` (
  `albumid` int(8) NOT NULL AUTO_INCREMENT,
  `albumname` varchar(50) CHARACTER SET gb2312 NOT NULL,
  `dateline` datetime NOT NULL,
  `picnum` smallint(6) NOT NULL,
  `pic` varchar(60) CHARACTER SET gb2312 NOT NULL,
  PRIMARY KEY (`albumid`)
) ENGINE=MyISAM  DEFAULT CHARSET=gbk AUTO_INCREMENT=7 ;
滇ICP备16003699号-2
最后修改:2018 年 09 月 05 日
如果觉得我的文章对你有用,请随意赞赏