php文件上傳、下載和刪除示例
php文件上傳、下載和刪除示例大體思路如下,具體內(nèi)容如下
一.文件上傳
1.把上傳文件的區(qū)域做出來
div1
2.把顯示文件的區(qū)域做出來
div2
3.提交表單,上傳文件
4.服務(wù)器接收文件數(shù)據(jù)
用$_FILE[name]接收
5.處理數(shù)據(jù),看上傳文件是否有錯(cuò)誤
錯(cuò)誤有如下幾種:
1).上傳的文件超過了 php.ini 中 upload_max_filesize 選項(xiàng)限制的值
2).上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值
3).文件只有部分被上傳
4).沒有文件被上傳
5).找不到臨時(shí)文件夾
6).文件寫入失敗
6.把上傳的文件從臨時(shí)文件夾移到指定文件夾存放
用這個(gè)move_uploaded_file函數(shù)
其中4 5 6步驟可以做成一個(gè)函數(shù)直接調(diào)用.
注意:文件上傳的頁面如果要嵌入php代碼,文件擴(kuò)展名不能是html,而是.php
二.文件下載
1.客戶端把文件名發(fā)送給服務(wù)器
2.服務(wù)器接收文件名,然后加上文件的路徑.
3.然后把文件數(shù)據(jù)傳回客戶端
一般是這四步:
//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í)行刪除文件操作
這里有一個(gè)圖片上傳下載刪除的小例子.
效果如圖:

文件上傳下載刪除的界面,代碼如下:
html+php內(nèi)嵌:
<!-- 選擇上傳文件區(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代碼,為了動(dòng)態(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代碼:
*{margin:0;padding:0;}
ul,li{list-style: none;}
/*最外層的div,目的是包住選擇文件按鈕,顯示框和上傳文件按鈕*/
#div1{width:405px;height:38px;position: relative;margin:40px auto;}
/*第二層div包住顯示框和上傳按鈕,右浮動(dòng)*/
#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)停留在選擇文件按鈕上的時(shí)候切換圖片*/
#div3:hover{background: url("upload.jpg") no-repeat 0 -40px;}
/*顯示圖片的div->ul,采用左浮動(dòng)的方式,一行行的排列圖片*/
#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(bǔ)標(biāo)簽的自帶樣式去掉,鼠標(biāo)停留時(shí)字體換顏色*/
#show-file ul li span,div a{text-decoration: none;color:orangered;}
#show-file ul li span,div a:hover{color: #00fa00;}
js代碼:
<script src="move.js"></script>
<script>
window.onload = function ()
{
//當(dāng)選擇文件后,會(huì)觸發(fā)這個(gè)事件
$('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}); //緩沖運(yùn)動(dòng)
startMove(aSpan[this.index],{opacity:100}); //緩沖運(yùn)動(dòng)
};
aLi[i].onmouseout = function ()
{
aSpan[this.index].style.display = 'none';
aDiv[this.index].style.display = 'none';
startMove(aDiv[this.index],{opacity:0}); //緩沖運(yùn)動(dòng)
startMove(aSpan[this.index],{opacity:0}); //緩沖運(yùn)動(dòng)
}
}
};
function $(id)
{
return document.getElementById(id);
}
</script>
處理上傳文件的php文件:
include('myFunctions.php');
if(uploadFile('file','upload'))
header("Location:upFileAndDownFile.php");//會(huì)馬上跳轉(zhuǎn)回原頁面,根本感覺不到頁面有跳轉(zhuǎn)到這里
處理下載文件的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完美運(yùn)動(dòng)框架文章有講過。
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.過濾上傳文件的錯(cuò)誤號(hào)
if($file['error'] > 0)
{
//獲取錯(cuò)誤信息
switch($file['error'])
{
case 1:
$info = '上傳的文件超過了 php.ini 中 upload_max_filesize 選項(xiàng)限制的值。';
break;
case 2:
$info = '上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值。';
break;
case 3:
$info = '文件只有部分被上傳。';
break;
case 4:
$info = '沒有文件被上傳。';
break;
case 6:
$info = '找不到臨時(shí)文件夾';
break;
case 7:
$info = '文件寫入失敗。 ';
break;
}
die("上傳錯(cuò)誤,原因: ".$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í)行文件上傳
//判斷是否是一個(gè)上傳文件
if(is_uploaded_file($file['tmp_name']))
{
//執(zhí)行文件上傳(移動(dòng)文件到指定目錄)
if(move_uploaded_file($file['tmp_name'],$path.'/'.$newfile))
return $path.'/'.$newfile;
else
return false;
}
else
die('不是一個(gè)上傳文件!');
}
上傳文件的時(shí)候注意要設(shè)置好HTML表單的大小限制和服務(wù)器的大小限制,post的大小限制。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- php使用ftp實(shí)現(xiàn)文件上傳與下載功能
- PHP操作FTP類 (上傳、下載、移動(dòng)、創(chuàng)建等)
- php下連接ftp實(shí)現(xiàn)文件的上傳、下載、刪除文件實(shí)例代碼
- PHP+FLASH實(shí)現(xiàn)上傳文件進(jìn)度條相關(guān)文件 下載
- php上傳apk后自動(dòng)提取apk包信息的使用(示例下載)
- php多文件上傳下載示例分享
- PHP實(shí)現(xiàn)文件上傳與下載實(shí)例與總結(jié)
- Flash兩個(gè)上傳示例ASP和PHP(原文件下載,包括后臺(tái)程序)
- PHP SFTP實(shí)現(xiàn)上傳下載功能
相關(guān)文章
用PHP獲取Google AJAX Search API 數(shù)據(jù)的代碼
用PHP獲取Google AJAX Search API 數(shù)據(jù)的代碼2010-03-03
使用PHP實(shí)現(xiàn)生成HTML靜態(tài)頁面
在PHP網(wǎng)站開發(fā)中為了網(wǎng)站推廣和SEO等需要,需要對(duì)網(wǎng)站進(jìn)行全站或局部靜態(tài)化處理,PHP生成靜態(tài)HTML頁面有多種方法,比如利用PHP模板、緩存等實(shí)現(xiàn)頁面靜態(tài)化,今天就以PHP實(shí)例教程形式討論P(yáng)HP生成靜態(tài)頁面的方法。2015-11-11
PHP實(shí)現(xiàn)算式驗(yàn)證碼和漢字驗(yàn)證碼實(shí)例
這篇文章主要介紹了PHP實(shí)現(xiàn)算式驗(yàn)證碼和漢字驗(yàn)證碼實(shí)例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-03-03
php基礎(chǔ)知識(shí):函數(shù)基礎(chǔ)知識(shí)
php基礎(chǔ)知識(shí):函數(shù)基礎(chǔ)知識(shí)...2006-12-12
php模擬用戶自動(dòng)在qq空間發(fā)表文章的方法
這篇文章主要介紹了php模擬用戶自動(dòng)在qq空間發(fā)表文章的方法,可實(shí)現(xiàn)模擬用戶提交表單發(fā)布文章的功能,代碼中包含有較為詳盡的注釋便于理解,需要的朋友可以參考下2015-01-01
PHP垃圾回收機(jī)制引用計(jì)數(shù)器概念分析
php變量存在一個(gè)叫"zval"的變量容器中,"zval"變量容器包括含變量的類型和值,還包括額外的兩個(gè)字節(jié)信息,分別是“is_ref”表示變量是否屬于引用,“refcount”指向這個(gè)zval變量容器的變量個(gè)數(shù)2013-06-06

