一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - PHP教程 - php文件上傳、下載和刪除示例

php文件上傳、下載和刪除示例

2021-03-13 17:01_acme_ PHP教程

這篇文章主要為大家詳細(xì)介紹了php文件上傳、下載和刪除示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

php文件上傳下載和刪除示例大體思路如下,具體內(nèi)容如下

一.文件上傳

1.把上傳文件的區(qū)域做出來

div1

2.把顯示文件的區(qū)域做出來

div2

3.提交表單,上傳文件

4.服務(wù)器接收文件數(shù)據(jù)

用$_FILE[name]接收

5.處理數(shù)據(jù),看上傳文件是否有錯誤

錯誤有如下幾種:

1).上傳的文件超過了 php.ini 中 upload_max_filesize 選項限制的值
2).上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值
3).文件只有部分被上傳
4).沒有文件被上傳
5).找不到臨時文件夾
6).文件寫入失敗

6.把上傳的文件從臨時文件夾移到指定文件夾存放

用這個move_uploaded_file函數(shù)
其中4 5 6步驟可以做成一個函數(shù)直接調(diào)用.
注意:文件上傳的頁面如果要嵌入php代碼,文件擴(kuò)展名不能是html,而是.php

二.文件下載

1.客戶端把文件名發(fā)送給服務(wù)器

2.服務(wù)器接收文件名,然后加上文件的路徑.

3.然后把文件數(shù)據(jù)傳回客戶端

一般是這四步:

?
1
2
3
4
5
6
7
8
9
//1.重設(shè)響應(yīng)類型
$info = getimagesize($rootPath.$file);
header("Content-Type:".$info['mime']);
//2.執(zhí)行下載的文件名
header("Content-Disposition:attachment;filename=".$file);
//3.指定文件大小
header("Content-Length:".filesize($rootPath.$file));
//4.響應(yīng)內(nèi)容
readfile($rootPath.$file);

三.文件刪除

1..客戶端把文件名發(fā)送給服務(wù)器

2.服務(wù)器接收文件名,然后加上文件的路徑.

3.用unlink函數(shù)執(zhí)行刪除文件操作

這里有一個圖片上傳下載刪除的小例子.
效果如圖:

php文件上傳、下載和刪除示例

文件上傳下載刪除的界面,代碼如下:
html+php內(nèi)嵌:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<!-- 選擇上傳文件區(qū)域-->
<div id="div1">
 <form action="upLoadFile.php" method="post" enctype="multipart/form-data">
  <div id="div2"><input type="text" id="show" /></div>
  <div id="div3">
   <span class="text">選擇文件</span>
    <input type='hidden' name='MAX_FILE_SIZE' value='100000000'> <!--表單上傳文件的大小限制<100M,也可以設(shè)置其它值-->
   <input type="file" id="upfile" name="file" />
  </div>
  <input type="submit" value="上傳" class="upload" />
 </form>
</div>
<!-- 選擇上傳文件區(qū)域結(jié)束-->
 
<!-- 上傳文件顯示區(qū)域-->
<div id="show-file">
 <ul id="ul-list">
  <!-- 內(nèi)嵌php代碼,為了動態(tài)顯示上傳的文件-->
  <?php
  //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.關(guān)閉目錄
  closedir($dir);
  ?>
  <!-- 內(nèi)嵌php代碼結(jié)束-->
 </ul>
</div>
<!-- 上傳文件顯示區(qū)域結(jié)束-->

css代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
*{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;}
 
  /*鼠標(biāo)停留在選擇文件按鈕上的時候切換圖片*/
  #div3:hover{background: url("upload.jpg") no-repeat 0 -40px;}
 
  /*顯示圖片的div->ul,采用左浮動的方式,一行行的排列圖片*/
  #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;}
 
  /*把a標(biāo)簽的自帶樣式去掉,鼠標(biāo)停留時字體換顏色*/
  #show-file ul li span,div a{text-decoration: none;color:orangered;}
  #show-file ul li span,div a:hover{color: #00fa00;}

js代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<script src="move.js"></script>
<script>
 window.onload = function ()
 {
  //當(dāng)選擇文件后,會觸發(fā)這個事件
  $('upfile').onchange = function ()
  {
   $('show').value = this.value;//把獲取到的文件偽路徑傳到編輯框
  };
  //顯示下載按鈕
  var aLi = $('ul-list').getElementsByTagName('li');  //圖片
  var aSpan = $('ul-list').getElementsByTagName('span'); //下載按鈕
  var aDiv = $('ul-list').getElementsByTagName('div'); //刪除按鈕
  for(var i = 0;i<aLi.length;i++)
  {
   aLi[i].index = i;
   aLi[i].onmousemove = function ()
   {
    aSpan[this.index].style.display = 'block';
    aDiv[this.index].style.display = 'block';
    startMove(aDiv[this.index],{opacity:100}); //緩沖運動
    startMove(aSpan[this.index],{opacity:100}); //緩沖運動
   };
   aLi[i].onmouseout = function ()
   {
    aSpan[this.index].style.display = 'none';
    aDiv[this.index].style.display = 'none';
    startMove(aDiv[this.index],{opacity:0}); //緩沖運動
    startMove(aSpan[this.index],{opacity:0}); //緩沖運動
   }
  }
 };
 function $(id)
 {
  return document.getElementById(id);
 }
</script>

處理上傳文件的php文件:

?
1
2
3
include('myFunctions.php');
if(uploadFile('file','upload'))
 header("Location:upFileAndDownFile.php");//會馬上跳轉(zhuǎn)回原頁面,根本感覺不到頁面有跳轉(zhuǎn)到這里

處理下載文件的php文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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中的函數(shù)如下:
 
/**
 * @function 下載文件
 * @param $file 要下載的文件名
 * @param $rootPath 文件根路徑
 * @return 無
 */
function downLoadFile($file,$rootPath)
{
 //1.重設(shè)響應(yīng)類型
 $info = getimagesize($rootPath.$file);
 header("Content-Type:".$info['mime']);
 //2.執(zhí)行下載的文件名
 header("Content-Disposition:attachment;filename=".$file);
 //3.指定文件大小
 header("Content-Length:".filesize($rootPath.$file));
 //4.響應(yīng)內(nèi)容
 readfile($rootPath.$file);
}
 
 
/**
 * @function 上傳文件
 * @param $name 表單名 <input type="file" name="pic" />
 * @param $path 上傳后,文件存放的路徑
 * @return 返回新的文件路徑表示上傳成功 false 失敗
 */
function uploadFile($name,$path)
{
 $file = $_FILES[$name];
 //1.過濾上傳文件的錯誤號
 if($file['error'] > 0)
 {
  //獲取錯誤信息
  switch($file['error'])
  {
   case 1:
    $info = '上傳的文件超過了 php.ini 中 upload_max_filesize 選項限制的值。';
    break;
   case 2:
    $info = '上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值。';
    break;
   case 3:
    $info = '文件只有部分被上傳。';
    break;
   case 4:
    $info = '沒有文件被上傳。';
    break;
   case 6:
    $info = '找不到臨時文件夾';
    break;
   case 7:
    $info = '文件寫入失敗。 ';
    break;
  }
  die("上傳錯誤,原因: ".$info);
 }
 //2.上傳文件大小的過濾
 if($file['size'] > 100000000) //字節(jié)為單位
  die('上傳文件大小超出限制!');
 //3.上傳后的文件名定義
 $newfile = null;
 $fileinfo = pathinfo($file['name']); //解析上傳文件名
 do{
  $newfile = date('YmdHis').".".$fileinfo['extension'];
 }while(file_exists($path.'/'.$newfile));
 //4.執(zhí)行文件上傳
 //判斷是否是一個上傳文件
 if(is_uploaded_file($file['tmp_name']))
 {
  //執(zhí)行文件上傳(移動文件到指定目錄)
  if(move_uploaded_file($file['tmp_name'],$path.'/'.$newfile))
   return $path.'/'.$newfile;
  else
   return false;
 }
 else
  die('不是一個上傳文件!');
}

上傳文件的時候注意要設(shè)置好HTML表單的大小限制和服務(wù)器的大小限制,post的大小限制。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 天天综合天天影视色香欲俱全 | 欧美一级免费看 | 26uuu成人人网图片 | 精品国产区一区二区三区在线观看 | 四虎影音在线 | 久久伊人精品青青草原2021 | 国产免费资源高清小视频在线观看 | 甜性涩爱 | 娇妻被又大又粗又长又硬好爽 | ady久久| 国产小视频在线免费观看 | 小仙夜晚慰自催眠mp3护士篇 | 精品国产区 | 欧美在线一 | 成人无高清96免费 | 好大~好爽~再进去一点 | 91进入蜜桃臀在线播放 | 免费深夜福利 | 亚洲一欧洲中文字幕在线 | 亚欧综合 | 99久久国产综合精品女不卡 | 欧美专区综合 | 美女把小内内脱个精光打屁屁 | 美女被的在线网站91 | 久久精品国产亚洲AV天美18 | 97伊人久久精品亚洲午夜 | 垫底辣妹免费观看完整版 | 草莓香蕉绿巨人丝瓜榴莲18 | 99久热只有精品视频免费观看17 | 国产一卡2卡3卡四卡精品网站 | 成人欧美1314www色视频 | katsuniav在线播放| 国产精品视频一区二区三区 | 公妇乱淫在线播放免费观看 | 亚洲性久久久影院 | a天堂中文在线 | coolgay男男gayxxx| www.9p234.com| 2048论坛永久入口 原创合集 | 99久热只有精品视频免费观看17 | 日本男男gayxxxxx免费 |