首頁 後端開發 php教程 php实现文件下载和多文件上传

php实现文件下载和多文件上传

Jun 23, 2016 pm 01:43 PM

文件下载:

html:

    <a href="1.rar">下载1.rar</a>    <br>    <a href="1.jpg">下载1.jpg</a> <!--会显示文件内容,而不是下载-->    <br>    <a href="doDownload.php?filename=1.jpg">通过程序下载1.jpg</a>    <br>    <a href="doDownload.php?filename=../upload/nv.jpg">下载nv.jpg</a>
登入後複製

php处理:

<?php $filename=$_GET['filename'];//设置下载文件名header('content-disposition:attachment;filename='.basename($filename));header('content-length:'.filesize($filename));readfile($filename);
登入後複製

文件上传:

html代码:

    
登入後複製
请选择您要上传的文件:
请选择您要上传的文件:
请选择您要上传的文件:
请选择您要上传的文件:
请选择您要上传的文件:

php代码:

common.func.php

<?php /** * 得到文件扩展名 * @param string $filename * @return string */function getExt($filename){	return strtolower(pathinfo($filename,PATHINFO_EXTENSION));}/** * 产生唯一字符串 * @return string */function getUniName(){	return md5(uniqid(microtime(true),true));}
登入後複製

upload.func1.php

<?php /** * 构建上传文件信息 * @return unknown */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){				$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;	}/** * 针对于单文件、多个单文件、多文件的上传 * @param array $fileInfo * @param string $path * @param string $flag * @param number $maxSize * @param array $allowExt * @return string */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	//判断错误号	if($fileInfo['error']===UPLOAD_ERR_OK){		//检测上传得到小		if($fileInfo['size']&gt;$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;	}}
登入後複製

doAction5.php

<?php //print_r($_FILES);header("content-type:text/html;charset=utf-8");require_once 'upload.func1.php';require_once 'common.func.php';$files=getFiles();// print_r($files);foreach($files as $fileInfo){	$res=uploadFile($fileInfo);	echo $res['mes'],'<br/>';	$uploadFiles[]=$res['dest'];}$uploadFiles=array_values(array_filter($uploadFiles));print_r($uploadFiles);
登入後複製

上面是通过函数实现,下载封装成为类:

html:

登入後複製
请选择您要上传的文件:

upload.class.php

<?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-&gt;maxSize=$maxSize;		$this-&gt;allowMime=$allowMime;		$this-&gt;allowExt=$allowExt;		$this-&gt;uploadPath=$uploadPath;		$this-&gt;imgFlag=$imgFlag;		$this-&gt;fileInfo=$_FILES[$this-&gt;fileName];	}	/**	 * 检测上传文件是否出错	 * @return boolean	 */	protected function checkError(){		if(!is_null($this-&gt;fileInfo)){			if($this-&gt;fileInfo['error']&gt;0){				switch($this-&gt;fileInfo['error']){					case 1:						$this-&gt;error='超过了PHP配置文件中upload_max_filesize选项的值';						break;					case 2:						$this-&gt;error='超过了表单中MAX_FILE_SIZE设置的值';						break;					case 3:						$this-&gt;error='文件部分被上传';						break;					case 4:						$this-&gt;error='没有选择上传文件';						break;					case 6:						$this-&gt;error='没有找到临时目录';						break;					case 7:						$this-&gt;error='文件不可写';						break;					case 8:						$this-&gt;error='由于PHP的扩展程序中断文件上传';						break;										}				return false;			}else{				return true;			}		}else{			$this-&gt;error='文件上传出错';			return false;		}	}	/**	 * 检测上传文件的大小	 * @return boolean	 */	protected function checkSize(){		if($this-&gt;fileInfo['size']&gt;$this-&gt;maxSize){			$this-&gt;error='上传文件过大';			return false;		}		return true;	}	/**	 * 检测扩展名	 * @return boolean	 */	protected function checkExt(){		$this-&gt;ext=strtolower(pathinfo($this-&gt;fileInfo['name'],PATHINFO_EXTENSION));		if(!in_array($this-&gt;ext,$this-&gt;allowExt)){			$this-&gt;error='不允许的扩展名';			return false;		}		return true;	}	/**	 * 检测文件的类型	 * @return boolean	 */	protected function checkMime(){		if(!in_array($this-&gt;fileInfo['type'],$this-&gt;allowMime)){			$this-&gt;error='不允许的文件类型';			return false;		}		return true;	}	/**	 * 检测是否是真实图片	 * @return boolean	 */	protected function checkTrueImg(){		if($this-&gt;imgFlag){			if(!@getimagesize($this-&gt;fileInfo['tmp_name'])){				$this-&gt;error='不是真实图片';				return false;			}			return true;		}	}	/**	 * 检测是否通过HTTP POST方式上传上来的	 * @return boolean	 */	protected function checkHTTPPost(){		if(!is_uploaded_file($this-&gt;fileInfo['tmp_name'])){			$this-&gt;error='文件不是通过HTTP POST方式上传上来的';			return false;		}		return true;	}	/**	 *显示错误 	 */	protected function showError(){		exit('<span style="color:red">'.$this-&gt;error.'</span>');	}	/**	 * 检测目录不存在则创建	 */	protected function checkUploadPath(){		if(!file_exists($this-&gt;uploadPath)){			mkdir($this-&gt;uploadPath,0777,true);		}	}	/**	 * 产生唯一字符串	 * @return string	 */	protected function getUniName(){		return md5(uniqid(microtime(true),true));	}	/**	 * 上传文件	 * @return string	 */	public function uploadFile(){		if($this-&gt;checkError()&amp;&amp;$this-&gt;checkSize()&amp;&amp;$this-&gt;checkExt()&amp;&amp;$this-&gt;checkMime()&amp;&amp;$this-&gt;checkTrueImg()&amp;&amp;$this-&gt;checkHTTPPost()){			$this-&gt;checkUploadPath();			$this-&gt;uniName=$this-&gt;getUniName();			$this-&gt;destination=$this-&gt;uploadPath.'/'.$this-&gt;uniName.'.'.$this-&gt;ext;			if(@move_uploaded_file($this-&gt;fileInfo['tmp_name'], $this-&gt;destination)){				return  $this-&gt;destination;			}else{				$this-&gt;error='文件移动失败';				$this-&gt;showError();			}		}else{			$this-&gt;showError();		}	}}
登入後複製

doAction6.php

<?php header('content-type:text/html;charset=utf-8');require_once 'upload.class.php';$upload=new upload('myFile1','imooc');$dest=$upload->uploadFile();echo $dest;
登入後複製


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

11個最佳PHP URL縮短腳本(免費和高級) 11個最佳PHP URL縮短腳本(免費和高級) Mar 03, 2025 am 10:49 AM

11個最佳PHP URL縮短腳本(免費和高級)

在Laravel中使用Flash會話數據 在Laravel中使用Flash會話數據 Mar 12, 2025 pm 05:08 PM

在Laravel中使用Flash會話數據

構建具有Laravel後端的React應用程序:第2部分,React 構建具有Laravel後端的React應用程序:第2部分,React Mar 04, 2025 am 09:33 AM

構建具有Laravel後端的React應用程序:第2部分,React

簡化的HTTP響應在Laravel測試中模擬了 簡化的HTTP響應在Laravel測試中模擬了 Mar 12, 2025 pm 05:09 PM

簡化的HTTP響應在Laravel測試中模擬了

php中的捲曲:如何在REST API中使用PHP捲曲擴展 php中的捲曲:如何在REST API中使用PHP捲曲擴展 Mar 14, 2025 am 11:42 AM

php中的捲曲:如何在REST API中使用PHP捲曲擴展

在Codecanyon上的12個最佳PHP聊天腳本 在Codecanyon上的12個最佳PHP聊天腳本 Mar 13, 2025 pm 12:08 PM

在Codecanyon上的12個最佳PHP聊天腳本

宣布 2025 年 PHP 形勢調查 宣布 2025 年 PHP 形勢調查 Mar 03, 2025 pm 04:20 PM

宣布 2025 年 PHP 形勢調查

Laravel中的通知 Laravel中的通知 Mar 04, 2025 am 09:22 AM

Laravel中的通知

See all articles