栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

php文件上传、下载和删除示例

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

php文件上传、下载和删除示例

php文件上传、下载和删除示例大体思路如下,具体内容如下

一.文件上传

1.把上传文件的区域做出来

div1

2.把显示文件的区域做出来

div2

3.提交表单,上传文件

4.服务器接收文件数据

用$_FILE[name]接收

5.处理数据,看上传文件是否有错误

错误有如下几种:

1).上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值
2).上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值
3).文件只有部分被上传
4).没有文件被上传
5).找不到临时文件夹
6).文件写入失败

6.把上传的文件从临时文件夹移到指定文件夹存放

用这个move_uploaded_file函数
其中4 5 6步骤可以做成一个函数直接调用.
注意:文件上传的页面如果要嵌入php代码,文件扩展名不能是html,而是.php

二.文件下载

1.客户端把文件名发送给服务器

2.服务器接收文件名,然后加上文件的路径.

3.然后把文件数据传回客户端

一般是这四步:

  //1.重设响应类型
 $info = getimagesize($rootPath.$file);
 header("Content-Type:".$info['mime']);
 //2.执行下载的文件名
 header("Content-Disposition:attachment;filename=".$file);
 //3.指定文件大小
 header("Content-Length:".filesize($rootPath.$file));
 //4.响应内容
 readfile($rootPath.$file);

三.文件删除

1..客户端把文件名发送给服务器

2.服务器接收文件名,然后加上文件的路径.

3.用unlink函数执行删除文件操作

这里有一个图片上传下载删除的小例子.
效果如图:

文件上传下载删除的界面,代码如下:
html+php内嵌:



 





 
    删除 下载"; } //3.关闭目录 closedir($dir); ?>

css代码:

*{margin:0;padding:0;}
  ul,li{list-style: none;}
  
  #div1{width:405px;height:38px;position: relative;margin:40px auto;}

  
  #div2{float: right;}
  #div2 input {width:250px;height: 38px;font-size: 22px;}

  
  #div3{float:left;width:140px;height:38px;position: relative;
   background: url("upload.jpg") no-repeat 0 0;margin-left: 5px;}
  #div3 input{position: absolute;width:100%;height: 100%;top:0;left: 0;
   z-index: 1;opacity:0;}

  
  .text{display: block;width:140px;height: 38px;position: absolute;top: 0;
   left:0;text-align: center;line-height: 38px;font-size: 28px;
   color: orchid;}

  
  .upload{width:70px;height: 38px;background: greenyellow;position: absolute;top:0;right: -75px;}

  
  #div3:hover{background: url("upload.jpg") no-repeat 0 -40px;}

  
  #show-file{width:760px;height:445px;position: relative;margin:10px auto;overflow: scroll;}
  #show-file ul{width:760px;height:445px;position: absolute;top:0;left:0;}
  #show-file ul li{float: left;width:120px;height: 100px;margin: 3px 0 0 3px;position: relative;}

  
  #show-file ul li div{display: none;opacity: 0;width:40px;height: 20px;position: absolute;left: 5px;bottom: 5px;
   background: gold;color: #d32a0e;z-index: 1;cursor: pointer;text-align: center;line-height: 20px;}

  
  #show-file ul li span{display: none;opacity: 0;width:40px;height: 20px;position: absolute;right: 5px;bottom: 5px;
   background: gold;color: #d32a0e;z-index: 1;cursor: pointer;text-align: center;line-height: 20px;}

  
  #show-file ul li span,div a{text-decoration: none;color:orangered;}
  #show-file ul li span,div a:hover{color: #00fa00;}

js代码: