• 在线相册管理(文件存放数据)


    公共函数库:functions.php

      1 <?php 
      2     //文件上传函数
      3     
      4     /**
      5      *    文件上传函数
      6      *    @param string $path     定义文件上传到的路径
      7      *    @param array  $upfile    上传文件的详细信息
      8      *    @param array  $typeList    允许上传的文件类型
      9                                 ($typeList = array("image/jpeg","image/png","image/gif");)
     10      *    @param int      $maxSize    允许上传的最大文件大小(默认为0,表示不限制大小)
     11      *    return array  $res        返回文件上传成功或失败的信息(数组中info存放上传文件成功或失败的信息
     12                                 error为false,表示上传失败!error为true表示上传成功)
     13      *                            $res = array(
     14                                                 "info"=>"",
     15                                                 "error"=>false
     16                                             );
     17      */
     18     
     19     function upload($path,$upfile,$typeList=array(),$maxSize=0){
     20         //定义存放返回信息的数组
     21         $res = array(
     22                 "info"=>"",
     23                 "error"=>false
     24                 );
     25         
     26         // 格式化文件上传路径信息
     27         $path = rtrim($path,"/")."/";
     28         
     29         //1.判断上传文件的错误号
     30         if($upfile['error']>0){
     31             switch($upfile['error']){
     32                 case 1:$info = "表示上传文件的大小超出了约定值!";break;
     33                 case 2:$info = "表示上传文件大小超出了HTML表单隐藏域属性的MAX_FILE_SIZE元素所指定的最大值。";break;
     34                 case 3:$info = "表示文件只被部分上传!";break;
     35                 case 4:$info = "表示没有上传任何文件。";break;
     36                 case 6:$info = "表示找不到临时文件夹。";break;
     37                 case 7:$info = "表示文件写入失败。";break;
     38                 default:$info = "未知的文件上传你错误!";break;
     39             }
     40             $res['info'] = "上传失败!原因:".$info;
     41             return $res;
     42         }
     43         
     44         //2.判断文件的上传类型是否合法
     45         if(@$typeList && count(@$typeList)>0){
     46             
     47             //判断用户上传的文件类型是否包含在服务器允许的类型之中
     48             if(!in_array($upfile['type'],$typeList)){
     49                 $res['info'] = "上传失败!原因:不被允许的上传文件类型!";
     50                 return $res;
     51             }
     52         }else{
     53             $res['info'] = "上传失败!原因:服务器没有设定允许上传的文件类型!";
     54             return $res;
     55         }
     56         
     57         //3.判断上传文件的大小是否合法
     58         if($maxSize>0 && $upfile['size']>$maxSize){
     59             $res['info'] = "上传失败!原因:上传文件大小越界!";
     60             return $res;
     61         }
     62         
     63         //4.随机一个文件名称
     64         $pathinfo = pathinfo($upfile['name']);    //获取上传那文件的文件名的详细信息
     65         $ext = $pathinfo['extension'];    //获取文件后缀名
     66         do{
     67             $newname = date("YmdHis",time()).rand(1000,9999).".".$ext;    //拼装随机文件名
     68         }while(file_exists($path.$newname));
     69         
     70         //5.执行上传文件的移动
     71         if(is_uploaded_file($upfile['tmp_name'])){
     72             
     73             //判断上传文件移动是否成功
     74             if(move_uploaded_file($upfile['tmp_name'],$path.$newname)){
     75                 $res['info'] = $newname;
     76                 $res['error'] = true;
     77                 return $res;
     78             }else{
     79                 $res['info'] = "上传失败!原因:移动上传文件失败!";
     80                 return $res;
     81             }
     82             
     83         }else{
     84             $res['info'] = "上传失败!原因:不是有效的上传文件!";
     85             return $res;
     86         }
     87     }
     88     
     89     
     90     /**
     91      *    图像的等比例缩放函数
     92      *    @param string $path     图片进行缩放之后的存储路径(原图也在这个目录下)
     93      *    @param string $picname    图片的名称(你要进行缩放的图片的名称)
     94      *    @param int      $maxw        图片缩放之后的最大宽度
     95      *    @param int      $maxh        图片缩放之后的最大高度
     96      *    @param string $pre        图片缩放之后存储的新图片的前缀名
     97      *    return bool      TRUE        图片缩放成功之后返回布尔型真 True
     98      */
     99     function imageResize($path,$picname,$maxw=100,$maxh=100,$pre="s_"){
    100     //1.准备画布
    101         //判断是否传入了合法的路径和图片名称
    102         if(empty($path) || empty($picname)){
    103             return false;
    104         }
    105         
    106         //格式化一下路径信息
    107         $path = rtrim($path,"/")."/";
    108     
    109         //获取图片的详细信息
    110         $info = getimagesize($path.$picname);
    111         
    112         //判断图片的类型,并生成相应的图片画布
    113         switch($info[2]){
    114             case 1:    //gif
    115                 $oldImg = imagecreatefromgif($path.$picname);
    116             break;
    117             case 2:    //jpeg
    118                 $oldImg = imagecreatefromjpeg($path.$picname);
    119             break;
    120             case 3:    //png
    121                 $oldImg = imagecreatefrompng($path.$picname);
    122             break;
    123         }
    124         
    125         //获取原图的宽高
    126         $oldw = imagesx($oldImg);
    127         $oldh = imagesy($oldImg);
    128         
    129         //判断原图的宽高,并进行比例的计算
    130         if($oldw>$oldh){
    131             //求得比例
    132             $bl = $oldw/$maxw;
    133             //计算缩放之后的宽高
    134             $neww = $oldw/$bl;
    135             $newh = $oldh/$bl;
    136         }else{
    137             //求得比例
    138             $bl = $oldh/$maxh;
    139             //计算缩放之后的宽高
    140             $neww = $oldw/$bl;
    141             $newh = $oldh/$bl;
    142         }
    143         
    144         //根据缩放之后的新的宽高,来生成一张新的画布
    145         $newImg = imagecreatetruecolor($neww,$newh);
    146     
    147     //2.开始绘画
    148         imagecopyresampled($newImg,$oldImg,0,0,0,0,$neww,$newh,$oldw,$oldh);
    149     
    150     //3.输出图像
    151         //判断原图的类型,并执行相应类型的输出
    152         switch($info[2]){
    153             case 1:    //gif
    154                 imagegif($newImg,$path.$pre.$picname);
    155             break;
    156             case 2:    //jpeg
    157                 imagejpeg($newImg,$path.$pre.$picname);
    158             break;
    159             case 3:    //png
    160                 imagepng($newImg,$path.$pre.$picname);
    161             break;
    162         }
    163     
    164     //4.释放资源
    165         imagedestroy($oldImg);
    166         imagedestroy($newImg);
    167         
    168         return true;
    169     }
    170 ?>

    公共菜单栏:menu.php

    1 <h1>在线相册管理系统</h1>
    2     <a href='./index.php'>添加相册</a> |
    3     <a href='./show.php'>查看相册</a>
    4 <hr/>

    主页:index.php

     1 <!DOCTYPE html>
     2 <html>
     3     <head>
     4         <title>在线相册管理系统</title>
     5         <meta charset="utf-8"/>
     6     </head>
     7     <body>
     8         <center>
     9             <?php 
    10                 require("./menu.php");
    11             ?>
    12             <h2>添加相册</h2>
    13             <form action="doAdd.php" method="post" enctype="multipart/form-data">
    14                 标题:<input type="text" name="title" /><br/><br/>
    15                 封面:<input type="file" name="upic" /><br/><br/>
    16                 <input type="submit" value="添加"/>
    17                 <input type="reset" value="重置"/>
    18             </form>
    19         </center>
    20     </body>
    21 </html>

    添加页:doAdd.php

     1 <!DOCTYPE html>
     2 <html>
     3     <head>
     4         <title>在线相册管理系统</title>
     5         <meta charset="utf-8"/>
     6     </head>
     7     <body>
     8         <center>
     9             <?php 
    10                 require("./menu.php");
    11             ?>
    12             <h2>添加相册</h2>
    13             <?php 
    14                 //执行相册添加的过程
    15                 //引入公共函数库
    16                 require("./functions.php");
    17                 
    18                 //定义必须的变量
    19                 $path = "./uploads";
    20                 $upfile = $_FILES['upic'];
    21                 $typeList = array("image/jpeg","image/png","image/gif");
    22                 $maxSize = 0;
    23                 
    24                 //执行上传
    25                 $res = upload($path,$upfile,$typeList,$maxSize);
    26                 
    27                 //判断是否上传成功
    28                 if($res['error']==false){
    29                     die($res['info']);
    30                 }
    31                 
    32                 //获取上传成功之后的文件名
    33                 $picname = $res['info'];
    34                 
    35                 //将图片进行等比例缩放
    36                 imageResize($path,$picname,100,100,"s_");
    37                 imageResize($path,$picname,300,300,"m_");
    38                 
    39                 $data = array();
    40                 //将相册的信息存入pic.db数据库文件(标题,图片原名,图片新名,类型,大小,添加时间)
    41                 $data['title'] = $_POST['title'];
    42                 $data['oldname'] = $_FILES['upic']['name'];
    43                 $data['newname'] = $picname;
    44                 $data['type'] = $_FILES['upic']['type'];
    45                 $data['size'] = $_FILES['upic']['size'];
    46                 $data['time'] = date("Y-m-d H:i:s",time());
    47                 
    48                 //将数据使用##进行拼装,并按@@对每条信息进行拼装
    49                 $contents = implode("##",$data)."@@";
    50                 
    51                 //将拼装好的相册信息存入数据库文件 pic.db
    52                 file_put_contents("./pic.db",$contents,FILE_APPEND);
    53                 
    54                 //告诉用户,添加相册成功!
    55                 echo "恭喜,添加成功!";
    56                 echo "<a href='show.php'>返回查看</a>";
    57             ?>
    58         </center>
    59     </body>
    60 </html>

    显示页:show.php

     1 <!DOCTYPE html>
     2 <html>
     3     <head>
     4         <title>在线相册管理系统</title>
     5         <meta charset="utf-8"/>
     6     </head>
     7     <body>
     8         <center>
     9             <?php 
    10                 require("./menu.php");
    11             ?>
    12             <h2>查看相册</h2>
    13             <table border='1' width="1000">
    14                 <tr>
    15                     <th>标题</th>
    16                     <th>图片名</th>
    17                     <th>类型</th>
    18                     <th>大小</th>
    19                     <th>时间</th>
    20                     <th>操作</th>
    21                 </tr>
    22                 <?php 
    23                     //将数据库中的所有数据读出来
    24                     $contents = file_get_contents("./pic.db");
    25                     
    26                     //去掉右侧多余的@@
    27                     $contents = rtrim($contents,"@@");
    28                     
    29                     //按@@进行拆分
    30                     $list = explode("@@",$contents);
    31                     
    32                     //遍历拆分以一条数据
    33                     foreach($list as $k=>$v){
    34                             if(!empty($v)){
    35                             //按每一条用##进行拆分
    36                             $list = explode("##",$v);
    37                             
    38                             //将数据放入表格
    39                             echo "<tr align='center'>";
    40                                 echo "<td>{$list[0]}</td>";
    41                                 echo "<td><a href='./uploads/{$list[2]}'><img src='./uploads/s_{$list[2]}'/></a></td>";
    42                                 echo "<td>{$list[3]}</td>";
    43                                 echo "<td>{$list[4]}</td>";
    44                                 echo "<td>{$list[5]}</td>";
    45                                 echo "<td>
    46                                           <a href='doDel.php?k={$k}&picname={$list[2]}'>删除</a>
    47                                           <a href='downLoad.php?filetype={$list[3]}&oldname={$list[1]}&size={$list[4]}&newname={$list[2]}&'>下载</a>
    48                                       </td>";
    49                             echo "</tr>";
    50                         }else{
    51                             echo "<tr><td colspan='6' align='center'>没有查到任何数据</td></tr>";
    52                         }
    53                     }
    54                     
    55                 ?>
    56             </table>
    57         </center>
    58     </body>
    59 </html>

    删除页:doDel.php

     1 <?php 
     2     //执行相册信息的删除
     3     
     4     //从pic.db数据库中取出所有的信息
     5     $contents = file_get_contents("./pic.db");
     6     
     7     //去掉右侧的@@
     8     $contents = rtrim($contents,"@@");
     9     
    10     //将相册信息按每条拆分开
    11     $list = explode("@@",$contents);
    12     
    13     //获取要删除的数据的下标
    14     $k = $_GET['k'];
    15     
    16     //获取要删除的文件名
    17     $picname = $_GET['picname'];
    18     
    19     //删除指定下标的信息
    20     unset($list[$k]);
    21     unlink("./uploads/".$picname);
    22     unlink("./uploads/s_".$picname);
    23     unlink("./uploads/m_".$picname);
    24     
    25     //判断我们相册信息是否还有
    26     if(count($list)>0){
    27         //将每条相册信息按@@拼装
    28         $contents = implode("@@",$list)."@@";
    29         //覆盖写回到数据库
    30         file_put_contents("./pic.db",$contents);
    31     }else{
    32         //将数据库信息置空
    33         file_put_contents("./pic.db","");
    34     }
    35     
    36     //直接跳回到查看相册信息的页面
    37     header("location:show.php");
    38 ?>

    下载页:downLoad.php

     1 <?php 
     2     //执行文件下载的页面
     3     
     4     //指定下载文件的类型
     5     header("Content-Type: {$_GET['filetype']}"); 
     6     
     7     //指定下载文件的描述信息
     8     header("Content-Disposition:attachment;filename=".$_GET['newname']);
     9     
    10     //指定下载文件的大小
    11     header("Content-Length:".$_GET['size']); 
    12     
    13     //将内容输出,以便下载。
    14     readfile("./uploads/".$_GET['newname']);
    15 
    16 ?>
  • 相关阅读:
    VUE-路由配置及跳转方式
    VUE使用axios请求后端数据
    springboot图片/文件上传
    java中return;语句的作用
    使用maven搭建ssm框架环境
    Java和Tomcat安装教程
    安装tomcat出现的问题
    关于 == 和 equals() 的区别
    关于从request对象中获取路径的问题
    栈和队列_leetcode20
  • 原文地址:https://www.cnblogs.com/yexiang520/p/5557979.html
Copyright © 2020-2023  润新知