《PHP實(shí)例:PHP實(shí)現(xiàn)文件上傳與下載實(shí)例與總結(jié)》要點(diǎn):
本文介紹了PHP實(shí)例:PHP實(shí)現(xiàn)文件上傳與下載實(shí)例與總結(jié),希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP教程一、上傳原理與配置
PHP教程1.1 原理
PHP教程將客戶端文件上傳到服務(wù)器端,再將服務(wù)器端的文件(臨時(shí)文件)移動(dòng)到指定目錄即可.
PHP教程1.2 客戶端配置
PHP教程所需:表單頁面(選擇上傳文件);
PHP教程具體而言:發(fā)送方式為POST,添加enctype="multipart/form-data"屬性,兩者缺一弗成(但是,優(yōu)缺點(diǎn)并存,這里也限定了上傳的方式和上傳的文件之后的調(diào)用等方面,后面會(huì)說到)
PHP教程
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="doAction.php" method="post" enctype="multipart/form-data">
請選擇您要上傳的文件:
<input type="file" name="myFile" /><br/>
<input type="submit" value="上傳"/>
</form>
<?php
?>
</body>
</html>
PHP教程先是表單頁面(請自動(dòng)忽略前端問題...),關(guān)鍵便是form的屬性;另外便是input 中用到了type="file"這一點(diǎn)(體現(xiàn)到php的強(qiáng)大的拓展等等).
PHP教程然后是doAction
PHP教程
<?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'];
//將服務(wù)器上的臨時(shí)文件移動(dòng)到指定位置
//辦法一move_upload_file($tmp_name,$destination)
//move_uploaded_file($tmp_name, "uploads/".$filename);//文件夾應(yīng)提前建立好,不然報(bào)錯(cuò)
//辦法二copy($src,$des)
//以上兩個(gè)函數(shù)都是成功返回真,否則返回false
//copy($tmp_name, "copies/".$filename);
//注意,不能兩個(gè)辦法都對臨時(shí)文件進(jìn)行操作,臨時(shí)文件似乎操作完就沒了,我們試試反過來
copy($tmp_name, "copies/".$filename);
move_uploaded_file($tmp_name, "uploads/".$filename);
//能夠?qū)崿F(xiàn),說明move那個(gè)函數(shù)基本上相當(dāng)于剪切;copy就是copy,臨時(shí)文件還在
//另外,錯(cuò)誤信息也是不一樣的,遇到錯(cuò)誤可以查看或者直接報(bào)告給用戶
if ($error==0) {
echo "上傳成功!";
}else{
switch ($error){
case 1:
echo "超過了上傳文件的最大值,請上傳2M以下文件";
break;
case 2:
echo "上傳文件過多,請一次上傳20個(gè)及以下文件!";
break;
case 3:
echo "文件并未完全上傳,請?jiān)俅螄L試!";
break;
case 4:
echo "未選擇上傳文件!";
break;
case 5:
echo "上傳文件為0";
break;
}
}
PHP教程先把print_r($_FILES)這個(gè)信息看一下
PHP教程
Array
(
[myFile] => Array
(
[name] => 梁博_簡歷.doc
[type] => application/msword
[tmp_name] => D:\wamp\tmp\php1D78.tmp
[error] => 0
[size] => 75776
)
)
PHP教程所以獲得的是個(gè)二維數(shù)組,該怎么用,都是基本的東西(其實(shí)我喜歡降維再用);
PHP教程基本是一眼就懂的器械,不羅嗦,關(guān)鍵有兩個(gè):tmp_name臨時(shí)文件名;error報(bào)錯(cuò)信息(代號(hào),后面可以利用);
PHP教程然后這里看一下doAction后面一部分,利用報(bào)錯(cuò)信息來反饋給用戶,必要說明的是為什么報(bào)錯(cuò),和報(bào)錯(cuò)信息是什么都;
PHP教程1.3 關(guān)于報(bào)錯(cuò)
PHP教程--報(bào)錯(cuò)原因
PHP教程基本上都是跨越或者不符合服務(wù)器關(guān)于上傳文件的配置,那么服務(wù)器端配置有哪些呢?
PHP教程先考慮上傳我們用了什么?POST,upload
PHP教程所以在php.ini中找這么幾項(xiàng):
PHP教程
file_upload:On
upload_tmp_dir=――臨時(shí)文件保留目錄;
upload_max_filesize=2M
max_file_uploads=20――允許一次上傳的最大文件數(shù)量(注意和上面那個(gè)的區(qū)別,有沒有size,別亂想)
post_max_size=8M――post方式發(fā)送數(shù)據(jù)的最大值
PHP教程其他相關(guān)配置
PHP教程max_exectuion_time=-1――最大執(zhí)行時(shí)間,避免程序欠好占用服務(wù)器資源;
PHP教程max_input_time=60
PHP教程max_input_nesting_level=64――輸入嵌套深度;
PHP教程memory_limit=128M――最年夜單線程的獨(dú)立內(nèi)存使用量
PHP教程總之都是有關(guān)資源的配置.
PHP教程--差錯(cuò)號(hào)
PHP教程以下(偷懶)引自http://blog.sina.com.cn/s/blog_3cdfaea201008utf.html
PHP教程UPLOAD_ERR_OK???????????? 值:0; 沒有錯(cuò)誤發(fā)生,文件上傳成功.
? UPLOAD_ERR_INI_SIZE????? 值:1; 上傳的文件超過了 php.ini 中 upload_max_filesize 選項(xiàng)限制的值.
? UPLOAD_ERR_FORM_SIZE? 值:2; 上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值.
? UPLOAD_ERR_PARTIAL????????? 值:3; 文件只有部門被上傳.
? UPLOAD_ERR_NO_FILE????????? 值:4; 沒有文件被上傳.?
PHP教程注意:這個(gè)錯(cuò)誤信息是第一步上傳的信息,也便是上傳到臨時(shí)文件夾的情況,而不是move或者copy的情況.
PHP教程二、上傳相關(guān)限制
PHP教程2.1 客戶端限制
PHP教程
<form action="doAction2.php" 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>
PHP教程這里用input的屬性對上傳文件的年夜小和類型進(jìn)行了限制,但是個(gè)人感覺:一,html代碼是“可見的”;二,常不起作用(沒找到原因,但因?yàn)榈谝粋€(gè)我也想放棄它,知道就好.
PHP教程?2.2 服務(wù)器端限制
PHP教程主要限制大小和類型,再有便是方式.
PHP教程
<?php
header('content-type:text/html;charset=utf-8');
//接受文件,臨時(shí)文件信息
$fileinfo=$_FILES["myFile"];//降維操作
$filename=$fileinfo["name"];
$tmp_name=$fileinfo["tmp_name"];
$size=$fileinfo["size"];
$error=$fileinfo["error"];
$type=$fileinfo["type"];
//服務(wù)器端設(shè)定限制
$maxsize=10485760;//10M,10*1024*1024
$allowExt=array('jpeg','jpg','png','tif');//允許上傳的文件類型(拓展名
$ext=pathinfo($filename,PATHINFO_EXTENSION);//提取上傳文件的拓展名
//目的信息
$path="uploads";
if (!file_exists($path)) { //當(dāng)目錄不存在,就創(chuàng)建目錄
mkdir($path,0777,true);
chmod($path, 0777);
}
//$destination=$path."/".$filename;
//得到唯一的文件名!防止因?yàn)槲募嗤a(chǎn)生覆蓋
$uniName=md5(uniqid(microtime(true),true)).$ext;//md5加密,uniqid產(chǎn)生唯一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)) {//@錯(cuò)誤抑制符,不讓用戶看到警告
echo "文件".$filename."上傳成功!";
}else{
echo "文件".$filename."上傳失敗!";
}
//判斷是否為真實(shí)圖片(防止偽裝成圖片的病毒一類的
if (!getimagesize($tmp_name)) {//getimagesize真實(shí)返回?cái)?shù)組,否則返回false
exit("不是真正的圖片類型");
}
}else{
switch ($error){
case 1:
echo "超過了上傳文件的最大值,請上傳2M以下文件";
break;
case 2:
echo "上傳文件過多,請一次上傳20個(gè)及以下文件!";
break;
case 3:
echo "文件并未完全上傳,請?jiān)俅螄L試!";
break;
case 4:
echo "未選擇上傳文件!";
break;
case 7:
echo "沒有臨時(shí)文件夾";
break;
}
}
PHP教程這里,具體實(shí)現(xiàn)都有注釋,每一步其實(shí)都可以本身試試的,很有趣.
PHP教程2.3 封裝
PHP教程函數(shù)
PHP教程
<?php
function uploadFile($fileInfo,$path,$allowExt,$maxSize){
$filename=$fileInfo["name"];
$tmp_name=$fileInfo["tmp_name"];
$size=$fileInfo["size"];
$error=$fileInfo["error"];
$type=$fileInfo["type"];
//服務(wù)器端設(shè)定限制
$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方式");
}
//判斷是否為真實(shí)圖片(防止偽裝成圖片的病毒一類的
if (!getimagesize($tmp_name)) {//getimagesize真實(shí)返回?cái)?shù)組,不然返回false
exit("不是真正的圖片類型");
}
if (@move_uploaded_file($tmp_name, $destination)) {//@錯(cuò)誤抑制符,不讓用戶看到警告
echo "文件".$filename."上傳成功!";
}else{
echo "文件".$filename."上傳失敗!";
}
}else{
switch ($error){
case 1:
echo "超過了上傳文件的最大值,請上傳2M以下文件";
break;
case 2:
echo "上傳文件過多,請一次上傳20個(gè)及以下文件!";
break;
case 3:
echo "文件并未完全上傳,請?jiān)俅螄L試!";
break;
case 4:
echo "未選擇上傳文件!";
break;
case 7:
echo "沒有臨時(shí)文件夾";
break;
}
}
return $destination;
}
PHP教程調(diào)用
PHP教程
<?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 'upFunc.php';
uploadFile($fileInfo, $path, $allowExt, $maxSize);
PHP教程三、多文件的上傳實(shí)現(xiàn)
PHP教程3.1 利用單文件封裝
PHP教程
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="doAction5.php" 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教程
<?php
//print_r($_FILES);
header('content-type:text/html;charset=utf-8');
include_once 'upFunc.php';
foreach ($_FILES as $fileInfo){
$file[]=uploadFile($fileInfo);
}
PHP教程這里的思路,從print_r($_FILES)中去找,打印出來看到是個(gè)二維數(shù)組,很簡單,遍歷去用就好了!
PHP教程上面那個(gè)function的界說改一下,給定一些默認(rèn)值
PHP教程function uploadFile($fileInfo,$path="uploads",$allowExt=array('jpeg','jpg','png','tif'),$maxSize=10485760){
PHP教程這樣子,簡單是簡單,但遇到一些問題.
PHP教程正常的上傳4個(gè)圖片是沒問題,但要是中間激活了函數(shù)中的exit,就會(huì)立即停止,導(dǎo)致其他圖片也無法上傳.
PHP教程3.2 升級版封裝
PHP教程旨在實(shí)現(xiàn)針對多個(gè)或單個(gè)文件上傳的封裝
PHP教程首先這樣子寫個(gè)靜態(tài)文件
PHP教程
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="doAction5.php" 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>
PHP教程打印一下$_FILES
PHP教程
Array
(
[myFile] => Array
(
[name] => Array
(
[0] => test32.png
[1] => test32.png
[2] => 333.png
[3] => test41.png
)
[type] => Array
(
[0] => image/png
[1] => image/png
[2] => image/png
[3] => image/png
)
[tmp_name] => Array
(
[0] => D:\wamp\tmp\php831C.tmp
[1] => D:\wamp\tmp\php834C.tmp
[2] => D:\wamp\tmp\php837C.tmp
[3] => D:\wamp\tmp\php83BB.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
[size] => Array
(
[0] => 46174
[1] => 46174
[2] => 34196
[3] => 38514
)
)
)
PHP教程可以獲得一個(gè)三維數(shù)組.
PHP教程復(fù)雜是復(fù)雜了,但復(fù)雜的有規(guī)律,各項(xiàng)數(shù)值都在一起了,很便利我們?nèi)≈?!
PHP教程所以先得到文件信息,釀成單文件處理那種信息
PHP教程
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){ //我的天,這個(gè)$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;
}
PHP教程然后之前的那種exit差錯(cuò),就把exit改一下就好了,這里用res
PHP教程
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
//判斷錯(cuò)誤號(hào)
$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'].'非法文件類型';
}
//檢測是否是真實(shí)的圖片類型
if($flag){
if(!getimagesize($fileInfo['tmp_name'])){
$res['mes']=$fileInfo['name'].'不是真實(shí)圖片類型';
}
}
//檢測文件是否是通過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'].'文件移動(dòng)失敗';
}
$res['mes']=$fileInfo['name'].'上傳成功';
$res['dest']=$destination;
return $res;
}else{
//匹配錯(cuò)誤信息
switch ($fileInfo ['error']) {
case 1 :
$res['mes'] = '上傳文件超過了PHP配置文件中upload_max_filesize選項(xiàng)的值';
break;
case 2 :
$res['mes'] = '超過了表單MAX_FILE_SIZE限制的大小';
break;
case 3 :
$res['mes'] = '文件部門被上傳';
break;
case 4 :
$res['mes'] = '沒有選擇上傳文件';
break;
case 6 :
$res['mes'] = '沒有找到臨時(shí)目錄';
break;
case 7 :
case 8 :
$res['mes'] = '系統(tǒng)錯(cuò)誤';
break;
}
return $res;
}
}
PHP教程里面封裝了兩個(gè)小的
PHP教程
function getExt($filename){
return strtolower(pathinfo($filename,PATHINFO_EXTENSION));
}
/**
* 發(fā)生唯一字符串
* @return string
*/
function getUniName(){
return md5(uniqid(microtime(true),true));
}
PHP教程然后靜態(tài)中,用multiple屬性實(shí)現(xiàn)多個(gè)文件的輸入;
PHP教程
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="doAction6.php" method="POST" enctype="multipart/form-data">
請選擇您要上傳的文件:<input type="file" name="myFile[]" multiple='multiple' /><br/>
<input type="submit" value="上傳"/>
</form>
</body>
</html>
PHP教程doAction6
PHP教程
<?php
//print_r($_FILES);
header("content-type:text/html;charset=utf-8");
require_once 'upFunc2.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);
PHP教程這樣子的幾個(gè)文件,就實(shí)現(xiàn)比擬強(qiáng)大的面向過程的上傳文件的功能(學(xué)的叫一個(gè)心酸...);
PHP教程四、面向工具的文件上傳
PHP教程?(不是很寫的動(dòng)了...先粘過來,再說吧...
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->maxSize=$maxSize;
$this->allowMime=$allowMime;
$this->allowExt=$allowExt;
$this->uploadPath=$uploadPath;
$this->imgFlag=$imgFlag;
$this->fileInfo=$_FILES[$this->fileName];
}
/**
* 檢測上傳文件是否出錯(cuò)
* @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選項(xiàng)的值';
break;
case 2:
$this->error='超過了表單中MAX_FILE_SIZE設(shè)置的值';
break;
case 3:
$this->error='文件部分被上傳';
break;
case 4:
$this->error='沒有選擇上傳文件';
break;
case 6:
$this->error='沒有找到臨時(shí)目錄';
break;
case 7:
$this->error='文件弗成寫';
break;
case 8:
$this->error='由于PHP的擴(kuò)展程序中斷文件上傳';
break;
}
return false;
}else{
return true;
}
}else{
$this->error='文件上傳出錯(cuò)';
return false;
}
}
/**
* 檢測上傳文件的大小
* @return boolean
*/
protected function checkSize(){
if($this->fileInfo['size']>$this->maxSize){
$this->error='上傳文件過大';
return false;
}
return true;
}
/**
* 檢測擴(kuò)展名
* @return boolean
*/
protected function checkExt(){
$this->ext=strtolower(pathinfo($this->fileInfo['name'],PATHINFO_EXTENSION));
if(!in_array($this->ext,$this->allowExt)){
$this->error='不允許的擴(kuò)展名';
return false;
}
return true;
}
/**
* 檢測文件的類型
* @return boolean
*/
protected function checkMime(){
if(!in_array($this->fileInfo['type'],$this->allowMime)){
$this->error='不允許的文件類型';
return false;
}
return true;
}
/**
* 檢測是否是真實(shí)圖片
* @return boolean
*/
protected function checkTrueImg(){
if($this->imgFlag){
if(!@getimagesize($this->fileInfo['tmp_name'])){
$this->error='不是真實(shí)圖片';
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;
}
/**
*顯示錯(cuò)誤
*/
protected function showError(){
exit('<span style="color:red">'.$this->error.'</span>');
}
/**
* 檢測目錄不存在則創(chuàng)建
*/
protected function checkUploadPath(){
if(!file_exists($this->uploadPath)){
mkdir($this->uploadPath,0777,true);
}
}
/**
* 產(chǎn)生唯一字符串
* @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='文件移動(dòng)失敗';
$this->showError();
}
}else{
$this->showError();
}
}
}
<?php
header('content-type:text/html;charset=utf-8');
require_once 'upload.class.php';
$upload=new upload('myFile1','imooc');
$dest=$upload->uploadFile();
echo $dest;
PHP教程四、下載
PHP教程對于瀏覽器不識(shí)別的,可以直接下載,但對于能識(shí)別的,必要多一兩步
PHP教程
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<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
?>
</body>
</html>
<?php
$filename=$_GET['filename'];
header('content-disposition:attachment;filename='.basename($filename));
header('content-length:'.filesize($filename));
readfile($filename);
PHP教程------------------總結(jié)-----------------------
PHP教程<form action="doAction.php" method="post" enctype="multipart/form-data">
?<input type="file" name="myFile" /><br/>
PHP教程?二維數(shù)組的降維處置;
PHP教程$_FILES變量
PHP教程move_upload_file();copy();
PHP教程tmp_name臨時(shí)文件;
PHP教程拓展名的提取;
PHP教程真實(shí)圖片的驗(yàn)證;
PHP教程唯一文件名的生成;
PHP教程函數(shù)封裝以及挪用;
PHP教程利用單個(gè)文件函數(shù)實(shí)現(xiàn)多文件上傳;
PHP教程小功能的封裝;
PHP教程多文件的遍歷;
PHP教程面向工具的開發(fā)過程;
歡迎參與《PHP實(shí)例:PHP實(shí)現(xiàn)文件上傳與下載實(shí)例與總結(jié)》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/7381.html