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.然后把文件数据传回客户端

一般是这四步:

rush:PHP;"> //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内嵌:

<div class="jb51code">
<pre class="brush:xhtml;">

<div id="div1">
<form action="upLoadFile.PHP" method="post" enctype="multipart/form-data">
<div id="div2">


<div id="div3">
<span class="text">选择文件

<div id="show-file">
<ul id="ul-list">

<?<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>
//1.打开目录
$dir = opendir('upload');
//2.遍历目录
$i = 0;
while($file = readdir($dir))
{
  if($file == '.'||$file == '..')
    continue;
  echo "<li><img src='upload/{$file}' width='120' height='100'>
    <div><a href='deleteFile.php?name={$file}'>删除</a></span></div>
    <span><a href='download.php?name={$file}'>下载</a></span></li>";
}
//3.<a href="https://www.jb51.cc/tag/guanbi/" target="_blank" class="keywords">关闭</a>目录
closedir($dir);
?>
<!-- 内嵌php代码结束-->

css代码:

<div class="jb51code">
<pre class="brush:css;">
{margin:0;padding:0;}
ul,li{list-style: none;}
/
最外层的div,目的是包住选择文件按钮,显示框和上传文件按钮*/

div1{width:405px;height:38px;position: relative;margin:40px auto;}

/*第二层div包住显示框和上传按钮,右浮动*/
#div2{float: right;}
#div2 input {width:250px;height: 38px;font-size: 22px;}

/*第三层div包住input file*/
#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;}

/*显示图片的div->ul,采用左浮动的方式,一行行的排列<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>*/
#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;}

/*<a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a>按钮的位置和一些样式*/
#show-file ul li div{<a href="https://www.jb51.cc/tag/dis/" target="_blank" class="keywords">dis</a>play: none;opacity: 0;width:40px;height: 20px;position: absolute;left: 5px;b<a href="https://www.jb51.cc/tag/ott/" target="_blank" class="keywords">ott</a>om: 5px;
  background: gold;color: #d32a0e;z-index: 1;cursor: pointer;text-align: center;line-height: 20px;}

/*下载按钮的位置和一些样式*/
#show-file ul li span{<a href="https://www.jb51.cc/tag/dis/" target="_blank" class="keywords">dis</a>play: none;opacity: 0;width:40px;height: 20px;position: absolute;right: 5px;b<a href="https://www.jb51.cc/tag/ott/" target="_blank" class="keywords">ott</a>om: 5px;
  background: gold;color: #d32a0e;z-index: 1;cursor: pointer;text-align: center;line-height: 20px;}

/*把a<a href="https://www.jb51.cc/tag/biaoqian/" target="_blank" class="keywords">标签</a>的<a href="https://www.jb51.cc/tag/zidai/" target="_blank" class="keywords">自带</a>样式去掉,鼠标停留时字体换颜色*/
#show-file ul li span,div a{text-<a href="https://www.jb51.cc/tag/decoration/" target="_blank" class="keywords">decoration</a>: none;color:orangered;}
#show-file ul li span,div a:hover{color: #00fa00;}

js代码:

rush:js;">

处理上传文件PHP文件:

rush:PHP;"> include('myFunctions.PHP'); if(uploadFile('file','upload')) header("Location:upFileAndDownFile.PHP");//会马上跳转回原页面,根本感觉不到页面跳转到这里

处理下载文件PHP文件:

rush:PHP;"> include('myFunctions.PHP'); //获取要下载的文件名(加上路径) $file = $_GET['name']; $rootPath = 'upload/'; downLoadFile($file,$rootPath);

处理删除文件PHP文件:

$fileName = 'upload/'.$_GET['name'];
unlink($fileName);
header("Location:upFileAndDownFile.PHP");

其中move.js在前面的JS完美运动框架文章有讲过。
myFunctions.PHP中的函数如下:

/**

/**

上传文件的时候注意要设置好HTML表单的大小限制和服务器的大小限制,post的大小限制。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

文件上传文件下载文件删除

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...