php實作檔案上傳下載下傳的方法:首先建立好表單頁面;然後将用戶端檔案上傳到伺服器端;最後将伺服器端的檔案移動到指定目錄即可。

推薦:《PHP視訊教程》
PHP實作檔案上傳與下載下傳
一、上傳原理與配置
原理
将用戶端檔案上傳到伺服器端,再将伺服器端的檔案(臨時檔案)移動到指定目錄即可。
用戶端配置
所需:(選擇上傳檔案);
具體而言:發送方式為POST,添加enctype="multipart/form-data"屬性,兩者缺一不可(但是,優缺點并存,這裡也限定了上傳的方式和上傳的檔案之後的調用等方面,後面會說到)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
請選擇您要上傳的檔案:
<input type="file" name="myFile" /><br/>
<input type="submit" value="上傳"/>
</form>
<?php
?>
</body>
</html>
先是表單頁面(請自動忽略前端問題。。。),關鍵就是form的屬性;另外就是input 中用到了type="file"這一點(展現到php的強大的拓展等等)。
然後是doAction
<?php
//$_FILES:檔案上傳變量
//print_r($_FILES);
$filename=$_FILES['myFile']['name'];
$type=$_FILES['myFile']['type'];
$tmp_name=$_FILES['myFile']['tmp_name'];
$size=$_FILES['myFile']['size'];
$error=$_FILES['myFile']['error'];
//将伺服器上的臨時檔案移動到指定位置
//方法一move_upload_file($tmp_name,$destination)
//move_uploaded_file($tmp_name, "uploads/".$filename);//檔案夾應提前建立好,不然報錯
//方法二copy($src,$des)
//以上兩個函數都是成功傳回真,否則傳回false
//copy($tmp_name, "copies/".$filename);
//注意,不能兩個方法都對臨時檔案進行操作,臨時檔案似乎操作完就沒了,我們試試反過來
copy($tmp_name, "copies/".$filename);
move_uploaded_file($tmp_name, "uploads/".$filename);
//能夠實作,說明move那個函數基本上相當于剪切;copy就是copy,臨時檔案還在
//另外,錯誤資訊也是不一樣的,遇到錯誤可以檢視或者直接報告給使用者
if ($error==0) {
echo "上傳成功!";
}else{
switch ($error){
case 1:
echo "超過了上傳檔案的最大值,請上傳2M以下檔案";
break;
case 2:
echo "上傳檔案過多,請一次上傳20個及以下檔案!";
break;
case 3:
echo "檔案并未完全上傳,請再次嘗試!";
break;
case 4:
echo "未選擇上傳檔案!";
break;
case 5:
echo "上傳檔案為0";
break;
}
}
先把print_r($_FILES)這個資訊看一下
Array
(
[myFile] => Array
(
[name] => 梁博_履歷.doc
[type] => application/msword
[tmp_name] => D:\wamp\tmp\
[error] => 0
[size] => 75776
)
)
是以得到的是個二維數組,該怎麼用,都是基本的東西(其實我喜歡降維再用);
基本是一眼就懂的東西,不羅嗦,關鍵有兩個:tmp_name臨時檔案名;error報錯資訊(代号,後面可以利用);
然後這裡看一下doAction後面一部分,利用報錯資訊來回報給使用者,需要說明的是為什麼報錯,和報錯資訊是什麼都;
關于報錯
--報錯原因:
基本上都是超過或者不符合伺服器關于上傳檔案的配置,那麼伺服器端配置有哪些呢?
先考慮上傳我們用了什麼?POST,upload
是以在中找這麼幾項:
file_upload:On
upload_tmp_dir=——臨時檔案儲存目錄;
upload_max_filesize=2M
max_file_uploads=20——允許一次上傳的最大檔案數量(注意和上面那個的差別,有沒有size,别亂想)
post_max_size=8M——post方式發送資料的最大值
其他相關配置
max_exectuion_time=-1——最大執行時間,避免程式不好占用伺服器資源;
max_input_time=60
max_input_nesting_level=64——輸入嵌套深度;
memory_limit=128M——最大單線程的獨立記憶體使用量
總之都是有關資源的配置。
--錯誤号
以下(偷懶)引自http://blog.sina.com.cn/s/blog_3cdfaea201008utf.html
- UPLOAD_ERR_OK 值:0; 沒有錯誤發生,檔案上傳成功。
- UPLOAD_ERR_INI_SIZE 值:1; 上傳的檔案超過了 中 upload_max_filesize 選項限制的值。
- UPLOAD_ERR_FORM_SIZE 值:2; 上傳檔案的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值。
- UPLOAD_ERR_PARTIAL 值:3; 檔案隻有部分被上傳。
- UPLOAD_ERR_NO_FILE 值:4; 沒有檔案被上傳。
注意:這個錯誤資訊是第一步上傳的資訊,也就是上傳到臨時檔案夾的情況,而不是move或者copy的情況。
二、上傳相關限制
用戶端限制
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="101321" />
請選擇您要上傳的檔案:
<input type="file" name="myFile" accept="image/jpeg,image/gif,text/html"/><br/>
<input type="submit" value="上傳"/>
</form>
這裡用input的屬性對上傳檔案的大小和類型進行了限制,但是個人感覺:一,html代碼是“可見的”;二,常不起作用(沒找到原因,但因為第一個我也想放棄它,知道就好。
伺服器端限制
主要限制大小和類型,再有就是方式。
<?php
header('content-type:text/html;charset=utf-8');
//接受檔案,臨時檔案資訊
$fileinfo=$_FILES["myFile"];//降維操作
$filename=$fileinfo["name"];
$tmp_name=$fileinfo["tmp_name"];
$size=$fileinfo["size"];
$error=$fileinfo["error"];
$type=$fileinfo["type"];
//伺服器端設定限制
$maxsize=10485760;//10M,10*1024*1024
$allowExt=array('jpeg','jpg','png','tif');//允許上傳的檔案類型(拓展名
$ext=pathinfo($filename,PATHINFO_EXTENSION);//提取上傳檔案的拓展名
//目的資訊
$path="uploads";
if (!file_exists($path)) { //當目錄不存在,就建立目錄
mkdir($path,0777,true);
chmod($path, 0777);
}
//$destination=$path."/".$filename;
//得到唯一的檔案名!防止因為檔案名相同而産生覆寫
$uniName=md5(uniqid(microtime(true),true)).$ext;//md5加密,uniqid産生唯一id,microtime做字首
if ($error==0) {
if ($size>$maxsize) {
exit("上傳檔案過大!");
}
if (!in_array($ext, $allowExt)) {
exit("非法檔案類型");
}
if (!is_uploaded_file($tmp_name)) {
exit("上傳方式有誤,請使用post方式");
}
if (@move_uploaded_file($tmp_name, $uniName)) {//@錯誤抑制符,不讓使用者看到警告
echo "檔案".$filename."上傳成功!";
}else{
echo "檔案".$filename."上傳失敗!";
}
//判斷是否為真實圖檔(防止僞裝成圖檔的病毒一類的
if (!getimagesize($tmp_name)) {//getimagesize真實傳回數組,否則傳回false
exit("不是真正的圖檔類型");
}
}else{
switch ($error){
case 1:
echo "超過了上傳檔案的最大值,請上傳2M以下檔案";
break;
case 2:
echo "上傳檔案過多,請一次上傳20個及以下檔案!";
break;
case 3:
echo "檔案并未完全上傳,請再次嘗試!";
break;
case 4:
echo "未選擇上傳檔案!";
break;
case 7:
echo "沒有臨時檔案夾";
break;
}
}
這裡,具體實作都有注釋,每一步其實都可以自己
封裝
函數
<?php
function uploadFile($fileInfo,$path,$allowExt,$maxSize){
$filename=$fileInfo["name"];
$tmp_name=$fileInfo["tmp_name"];
$size=$fileInfo["size"];
$error=$fileInfo["error"];
$type=$fileInfo["type"];
//伺服器端設定限制
$ext=pathinfo($filename,PATHINFO_EXTENSION);
//目的資訊
if (!file_exists($path)) {
mkdir($path,0777,true);
chmod($path, 0777);
}
$uniName=md5(uniqid(microtime(true),true)).'.'.$ext;
$destination=$path."/".$uniName;
if ($error==0) {
if ($size>$maxSize) {
exit("上傳檔案過大!");
}
if (!in_array($ext, $allowExt)) {
exit("非法檔案類型");
}
if (!is_uploaded_file($tmp_name)) {
exit("上傳方式有誤,請使用post方式");
}
//判斷是否為真實圖檔(防止僞裝成圖檔的病毒一類的
if (!getimagesize($tmp_name)) {//getimagesize真實傳回數組,否則傳回false
exit("不是真正的圖檔類型");
}
if (@move_uploaded_file($tmp_name, $destination)) {//@錯誤抑制符,不讓使用者看到警告
echo "檔案".$filename."上傳成功!";
}else{
echo "檔案".$filename."上傳失敗!";
}
}else{
switch ($error){
case 1:
echo "超過了上傳檔案的最大值,請上傳2M以下檔案";
break;
case 2:
echo "上傳檔案過多,請一次上傳20個及以下檔案!";
break;
case 3:
echo "檔案并未完全上傳,請再次嘗試!";
break;
case 4:
echo "未選擇上傳檔案!";
break;
case 7:
echo "沒有臨時檔案夾";
break;
}
}
return $destination;
}
調用
<?php
header('content-type:text/html;charset=utf-8');
$fileInfo=$_FILES["myFile"];
$maxSize=10485760;//10M,10*1024*1024
$allowExt=array('jpeg','jpg','png','tif');
$path="uploads";
include_once '';
uploadFile($fileInfo, $path, $allowExt, $maxSize);
三、多檔案的上傳實作
利用單檔案封裝
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
請選擇您要上傳的檔案:<input type="file" name="myFile1" /><br/>
請選擇您要上傳的檔案:<input type="file" name="myFile2" /><br/>
請選擇您要上傳的檔案:<input type="file" name="myFile3" /><br/>
請選擇您要上傳的檔案:<input type="file" name="myFile4" /><br/>
<input type="submit" value="上傳"/>
</form>
</body>
</html>
<?php
//print_r($_FILES);
header('content-type:text/html;charset=utf-8');
include_once '';
foreach ($_FILES as $fileInfo){
$file[]=uploadFile($fileInfo);
}
這裡的思路,從print_r($_FILES)中去找,列印出來看到是個二維數組,很簡單,周遊去用就好了!
上面那個function的定義改一下,給定一些預設值
function uploadFile($fileInfo,$path="uploads",$allowExt=array('jpeg','jpg','png','tif'),$maxSize=10485760){
這樣子,簡單是簡單,但遇到一些問題。
正常的上傳4個圖檔是沒問題,但要是中間激活了函數中的exit,就會立即停止,導緻其他圖檔也無法上傳。
更新版封裝
旨在實作針對多個或單個檔案上傳的封裝
首先這樣子寫個靜态檔案
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
請選擇您要上傳的檔案:<input type="file" name="myFile[]" /><br/>
請選擇您要上傳的檔案:<input type="file" name="myFile[]" /><br/>
請選擇您要上傳的檔案:<input type="file" name="myFile[]" /><br/>
請選擇您要上傳的檔案:<input type="file" name="myFile[]" /><br/>
<input type="submit" value="上傳"/>
</form>
</body>
</html>
列印一下$_FILES
Array
(
[myFile] => Array
(
[name] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
[type] => Array
(
[0] => image/png
[1] => image/png
[2] => image/png
[3] => image/png
)
[tmp_name] => Array
(
[0] => D:\wamp\tmp\
[1] => D:\wamp\tmp\
[2] => D:\wamp\tmp\
[3] => D:\wamp\tmp\
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
[size] => Array
(
[0] => 46174
[1] => 46174
[2] => 34196
[3] => 38514
)
)
)
可以得到一個三維數組。
複雜是複雜了,但複雜的有規律,各項數值都在一起了,很友善我們取值!!
是以先得到檔案資訊,變成單檔案處理那種資訊
function getFiles(){
$i=0;
foreach($_FILES as $file){
if(is_string($file['name'])){ //單檔案判定
$files[$i]=$file;
$i++;
}elseif(is_array($file['name'])){
foreach($file['name'] as $key=>$val){ //我的天,這個$key用的diao
$files[$i]['name']=$file['name'][$key];
$files[$i]['type']=$file['type'][$key];
$files[$i]['tmp_name']=$file['tmp_name'][$key];
$files[$i]['error']=$file['error'][$key];
$files[$i]['size']=$file['size'][$key];
$i++;
}
}
}
return $files;
}
然後之前的那種exit錯誤,就把exit改一下就好了,這裡用res
function uploadFile($fileInfo,$path='./uploads',$flag=true,$maxSize=1048576,$allowExt=array('jpeg','jpg','png','gif')){
//$flag=true;
//$allowExt=array('jpeg','jpg','gif','png');
//$maxSize=1048576;//1M
//判斷錯誤号
$res=array();
if($fileInfo['error']===UPLOAD_ERR_OK){
//檢測上傳得到小
if($fileInfo['size']>$maxSize){
$res['mes']=$fileInfo['name'].'上傳檔案過大';
}
$ext=getExt($fileInfo['name']);
//檢測上傳檔案的檔案類型
if(!in_array($ext,$allowExt)){
$res['mes']=$fileInfo['name'].'非法檔案類型';
}
//檢測是否是真實的圖檔類型
if($flag){
if(!getimagesize($fileInfo['tmp_name'])){
$res['mes']=$fileInfo['name'].'不是真實圖檔類型';
}
}
//檢測檔案是否是通過HTTP POST上傳上來的
if(!is_uploaded_file($fileInfo['tmp_name'])){
$res['mes']=$fileInfo['name'].'檔案不是通過HTTP POST方式上傳上來的';
}
if($res) return $res;
//$path='./uploads';
if(!file_exists($path)){
mkdir($path,0777,true);
chmod($path,0777);
}
$uniName=getUniName();
$destination=$path.'/'.$uniName.'.'.$ext;
if(!move_uploaded_file($fileInfo['tmp_name'],$destination)){
$res['mes']=$fileInfo['name'].'檔案移動失敗';
}
$res['mes']=$fileInfo['name'].'上傳成功';
$res['dest']=$destination;
return $res;
}else{
//比對錯誤資訊
switch ($fileInfo ['error']) {
case 1 :
$res['mes'] = '上傳檔案超過了PHP配置檔案中upload_max_filesize選項的值';
break;
case 2 :
$res['mes'] = '超過了表單MAX_FILE_SIZE限制的大小';
break;
case 3 :
$res['mes'] = '檔案部分被上傳';
break;
case 4 :
$res['mes'] = '沒有選擇上傳檔案';
break;
case 6 :
$res['mes'] = '沒有找到臨時目錄';
break;
case 7 :
case 8 :
$res['mes'] = '系統錯誤';
break;
}
return $res;
}
}
裡面封裝了兩個小的
function getExt($filename){
return strtolower(pathinfo($filename,PATHINFO_EXTENSION));
}
/**
* 産生唯一字元串
* @return string
*/
function getUniName(){
return md5(uniqid(microtime(true),true));
}
然後靜态中,用multiple屬性實作多個檔案的輸入;
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
請選擇您要上傳的檔案:<input type="file" name="myFile[]" multiple='multiple' /><br/>
<input type="submit" value="上傳"/>
</form>
</body>
</html>
doAction6
<?php
//print_r($_FILES);
header("content-type:text/html;charset=utf-8");
require_once '';
require_once '';
$files=getFiles();
// print_r($files);
foreach($files as $fileInfo){
$res=uploadFile($fileInfo);
echo $res['mes'],'<br/>';
$uploadFiles[][email protected]$res['dest'];
}
$uploadFiles=array_values(array_filter($uploadFiles));
//print_r($uploadFiles);
這樣子的幾個檔案,就實作比較強大的面向過程的上傳檔案的功能(學的叫一個心酸。。。);
四、面向對象的檔案上傳
<?php
class upload{
protected $fileName;
protected $maxSize;
protected $allowMime;
protected $allowExt;
protected $uploadPath;
protected $imgFlag;
protected $fileInfo;
protected $error;
protected $ext;
/**
* @param string $fileName
* @param string $uploadPath
* @param string $imgFlag
* @param number $maxSize
* @param array $allowExt
* @param array $allowMime
*/
public function __construct($fileName='myFile',$uploadPath='./uploads',$imgFlag=true,$maxSize=5242880,$allowExt=array('jpeg','jpg','png','gif'),$allowMime=array('image/jpeg','image/png','image/gif')){
$this->fileName=$fileName;
$this->maxSize=$maxSize;
$this->allowMime=$allowMime;
$this->allowExt=$allowExt;
$this->uploadPath=$uploadPath;
$this->imgFlag=$imgFlag;
$this->fileInfo=$_FILES[$this->fileName];
}
/**
* 檢測上傳檔案是否出錯
* @return boolean
*/
protected function checkError(){
if(!is_null($this->fileInfo)){
if($this->fileInfo['error']>0){
switch($this->fileInfo['error']){
case 1:
$this->error='超過了PHP配置檔案中upload_max_filesize選項的值';
break;
case 2:
$this->error='超過了表單中MAX_FILE_SIZE設定的值';
break;
case 3:
$this->error='檔案部分被上傳';
break;
case 4:
$this->error='沒有選擇上傳檔案';
break;
case 6:
$this->error='沒有找到臨時目錄';
break;
case 7:
$this->error='檔案不可寫';
break;
case 8:
$this->error='由于PHP的擴充程式中斷檔案上傳';
break;
}
return false;
}else{
return true;
}
}else{
$this->error='檔案上傳出錯';
return false;
}
}
/**
* 檢測上傳檔案的大小
* @return boolean
*/
protected function checkSize(){
if($this->fileInfo['size']>$this->maxSize){
$this->error='上傳檔案過大';
return false;
}
return true;
}
/**
* 檢測擴充名
* @return boolean
*/
protected function checkExt(){
$this->ext=strtolower(pathinfo($this->fileInfo['name'],PATHINFO_EXTENSION));
if(!in_array($this->ext,$this->allowExt)){
$this->error='不允許的擴充名';
return false;
}
return true;
}
/**
* 檢測檔案的類型
* @return boolean
*/
protected function checkMime(){
if(!in_array($this->fileInfo['type'],$this->allowMime)){
$this->error='不允許的檔案類型';
return false;
}
return true;
}
/**
* 檢測是否是真實圖檔
* @return boolean
*/
protected function checkTrueImg(){
if($this->imgFlag){
if([email protected]($this->fileInfo['tmp_name'])){
$this->error='不是真實圖檔';
return false;
}
return true;
}
}
/**
* 檢測是否通過HTTP POST方式上傳上來的
* @return boolean
*/
protected function checkHTTPPost(){
if(!is_uploaded_file($this->fileInfo['tmp_name'])){
$this->error='檔案不是通過HTTP POST方式上傳上來的';
return false;
}
return true;
}
/**
*顯示錯誤
*/
protected function showError(){
exit('<span style="color:red">'.$this->error.'</span>');
}
/**
* 檢測目錄不存在則建立
*/
protected function checkUploadPath(){
if(!file_exists($this->uploadPath)){
mkdir($this->uploadPath,0777,true);
}
}
/**
* 産生唯一字元串
* @return string
*/
protected function getUniName(){
return md5(uniqid(microtime(true),true));
}
/**
* 上傳檔案
* @return string
*/
public function uploadFile(){
if($this->checkError()&&$this->checkSize()&&$this->checkExt()&&$this->checkMime()&&$this->checkTrueImg()&&$this->checkHTTPPost()){
$this->checkUploadPath();
$this->uniName=$this->getUniName();
$this->destination=$this->uploadPath.'/'.$this->uniName.'.'.$this->ext;
if(@move_uploaded_file($this->fileInfo['tmp_name'], $this->destination)){
return $this->destination;
}else{
$this->error='檔案移動失敗';
$this->showError();
}
}else{
$this->showError();
}
}
}
<?php
header('content-type:text/html;charset=utf-8');
require_once '';
$upload=new upload('myFile1','imooc');
$dest=$upload->uploadFile();
echo $dest;
四、下載下傳
對于浏覽器不識别的,可以直接下載下傳,但對于能識别的,需要多一兩步
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<a href="" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >下載下傳</a>
<br />
<a href="" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >下載下傳</a>
<br />
<a href="doDownload.php?filename=" target="_blank" rel="external nofollow" >通過程式下載下傳</a>
<br />
<a href="" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >下載下傳nv.jpg</a>
<?php
?>
</body>
</html>
<?php
$filename=$_GET['filename'];
header('content-disposition:attachment;filename='.basename($filename));
header('content-length:'.filesize($filename));
readfile($filename);