PHP實(shí)現(xiàn)文件上傳下載實(shí)例
本文介紹了PHP實(shí)現(xiàn)文件上傳與下載,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。
一、上傳原理與配置
1.1 原理
將客戶端文件上傳到服務(wù)器端,再將服務(wù)器端的文件(臨時(shí)文件)移動(dòng)到指定目錄即可。
1.2 客戶端配置
所需:表單頁(yè)面(選擇上傳文件);
具體而言:發(fā)送方式為POST,添加enctype="multipart/form-data"屬性,兩者缺一不可(但是,優(yōu)缺點(diǎn)并存,這里也限定了上傳的方式和上傳的文件之后的調(diào)用等方面,后面會(huì)說(shuō)到)
<!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"> 請(qǐng)選擇您要上傳的文件: <input type="file" name="myFile" /><br/> <input type="submit" value="上傳"/> </form> <?php ?> </body> </html>
先是表單頁(yè)面(請(qǐng)自動(dòng)忽略前端問(wèn)題。。。),關(guān)鍵就是form的屬性;另外就是input 中用到了type="file"這一點(diǎn)(體現(xiàn)到php的強(qiáng)大的拓展等等)。
然后是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'];
//將服務(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è)方法都對(duì)臨時(shí)文件進(jìn)行操作,臨時(shí)文件似乎操作完就沒(méi)了,我們?cè)囋嚪催^(guò)來(lái)
copy($tmp_name, "copies/".$filename);
move_uploaded_file($tmp_name, "uploads/".$filename);
//能夠?qū)崿F(xiàn),說(shuō)明move那個(gè)函數(shù)基本上相當(dāng)于剪切;copy就是copy,臨時(shí)文件還在
//另外,錯(cuò)誤信息也是不一樣的,遇到錯(cuò)誤可以查看或者直接報(bào)告給用戶
if ($error==0) {
echo "上傳成功!";
}else{
switch ($error){
case 1:
echo "超過(guò)了上傳文件的最大值,請(qǐng)上傳2M以下文件";
break;
case 2:
echo "上傳文件過(guò)多,請(qǐng)一次上傳20個(gè)及以下文件!";
break;
case 3:
echo "文件并未完全上傳,請(qǐng)?jiān)俅螄L試!";
break;
case 4:
echo "未選擇上傳文件!";
break;
case 5:
echo "上傳文件為0";
break;
}
}
先把print_r($_FILES)這個(gè)信息看一下
Array
(
[myFile] => Array
(
[name] => 梁博_簡(jiǎn)歷.doc
[type] => application/msword
[tmp_name] => D:\wamp\tmp\php1D78.tmp
[error] => 0
[size] => 75776
)
)
所以得到的是個(gè)二維數(shù)組,該怎么用,都是基本的東西(其實(shí)我喜歡降維再用);
基本是一眼就懂的東西,不羅嗦,關(guān)鍵有兩個(gè):tmp_name臨時(shí)文件名;error報(bào)錯(cuò)信息(代號(hào),后面可以利用);
然后這里看一下doAction后面一部分,利用報(bào)錯(cuò)信息來(lái)反饋給用戶,需要說(shuō)明的是為什么報(bào)錯(cuò),和報(bào)錯(cuò)信息是什么都;
1.3 關(guān)于報(bào)錯(cuò)
--報(bào)錯(cuò)原因:
基本上都是超過(guò)或者不符合服務(wù)器關(guān)于上傳文件的配置,那么服務(wù)器端配置有哪些呢?
先考慮上傳我們用了什么?POST,upload
所以在php.ini中找這么幾項(xiàng):
file_upload:On
upload_tmp_dir=——臨時(shí)文件保存目錄;
upload_max_filesize=2M
max_file_uploads=20——允許一次上傳的最大文件數(shù)量(注意和上面那個(gè)的區(qū)別,有沒(méi)有size,別亂想)
post_max_size=8M——post方式發(fā)送數(shù)據(jù)的最大值
其他相關(guān)配置
max_exectuion_time=-1——最大執(zhí)行時(shí)間,避免程序不好占用服務(wù)器資源;
max_input_time=60
max_input_nesting_level=64——輸入嵌套深度;
memory_limit=128M——最大單線程的獨(dú)立內(nèi)存使用量
總之都是有關(guān)資源的配置。
--錯(cuò)誤號(hào)
以下(偷懶)引自http://blog.sina.com.cn/s/blog_3cdfaea201008utf.html
- UPLOAD_ERR_OK 值:0; 沒(méi)有錯(cuò)誤發(fā)生,文件上傳成功。
- UPLOAD_ERR_INI_SIZE 值:1; 上傳的文件超過(guò)了 php.ini 中 upload_max_filesize 選項(xiàng)限制的值。
- UPLOAD_ERR_FORM_SIZE 值:2; 上傳文件的大小超過(guò)了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值。
- UPLOAD_ERR_PARTIAL 值:3; 文件只有部分被上傳。
- UPLOAD_ERR_NO_FILE 值:4; 沒(méi)有文件被上傳。
注意:這個(gè)錯(cuò)誤信息是第一步上傳的信息,也就是上傳到臨時(shí)文件夾的情況,而不是move或者copy的情況。
二、上傳相關(guān)限制
2.1 客戶端限制
<form action="doAction2.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="101321" />
請(qǐng)選擇您要上傳的文件:
<input type="file" name="myFile" accept="image/jpeg,image/gif,text/html"/><br/> <input type="submit" value="上傳"/> </form>
這里用input的屬性對(duì)上傳文件的大小和類型進(jìn)行了限制,但是個(gè)人感覺(jué):一,html代碼是“可見(jiàn)的”;二,常不起作用(沒(méi)找到原因,但因?yàn)榈谝粋€(gè)我也想放棄它,知道就好。
2.2 服務(wù)器端限制
主要限制大小和類型,再有就是方式。
<?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("上傳文件過(guò)大!");
}
if (!in_array($ext, $allowExt)) {
exit("非法文件類型");
}
if (!is_uploaded_file($tmp_name)) {
exit("上傳方式有誤,請(qǐng)使用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 "超過(guò)了上傳文件的最大值,請(qǐng)上傳2M以下文件";
break;
case 2:
echo "上傳文件過(guò)多,請(qǐng)一次上傳20個(gè)及以下文件!";
break;
case 3:
echo "文件并未完全上傳,請(qǐng)?jiān)俅螄L試!";
break;
case 4:
echo "未選擇上傳文件!";
break;
case 7:
echo "沒(méi)有臨時(shí)文件夾";
break;
}
}
這里,具體實(shí)現(xiàn)都有注釋,每一步其實(shí)都可以自己
2.3 封裝
函數(shù)
<?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("上傳文件過(guò)大!");
}
if (!in_array($ext, $allowExt)) {
exit("非法文件類型");
}
if (!is_uploaded_file($tmp_name)) {
exit("上傳方式有誤,請(qǐng)使用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 "超過(guò)了上傳文件的最大值,請(qǐng)上傳2M以下文件";
break;
case 2:
echo "上傳文件過(guò)多,請(qǐng)一次上傳20個(gè)及以下文件!";
break;
case 3:
echo "文件并未完全上傳,請(qǐng)?jiān)俅螄L試!";
break;
case 4:
echo "未選擇上傳文件!";
break;
case 7:
echo "沒(méi)有臨時(shí)文件夾";
break;
}
}
return $destination;
}
調(diào)用
<?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);
三、多文件的上傳實(shí)現(xiàn)
3.1 利用單文件封裝
<!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">
請(qǐng)選擇您要上傳的文件:<input type="file" name="myFile1" /><br/>
請(qǐng)選擇您要上傳的文件:<input type="file" name="myFile2" /><br/>
請(qǐng)選擇您要上傳的文件:<input type="file" name="myFile3" /><br/>
請(qǐng)選擇您要上傳的文件:<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 'upFunc.php';
foreach ($_FILES as $fileInfo){
$file[]=uploadFile($fileInfo);
}
這里的思路,從print_r($_FILES)中去找,打印出來(lái)看到是個(gè)二維數(shù)組,很簡(jiǎn)單,遍歷去用就好了!
上面那個(gè)function的定義改一下,給定一些默認(rèn)值
function uploadFile($fileInfo,$path="uploads",$allowExt=array('jpeg','jpg','png','tif'),$maxSize=10485760){
這樣子,簡(jiǎn)單是簡(jiǎn)單,但遇到一些問(wèn)題。
正常的上傳4個(gè)圖片是沒(méi)問(wèn)題,但要是中間激活了函數(shù)中的exit,就會(huì)立即停止,導(dǎo)致其他圖片也無(wú)法上傳。
3.2 升級(jí)版封裝
旨在實(shí)現(xiàn)針對(duì)多個(gè)或單個(gè)文件上傳的封裝
首先這樣子寫個(gè)靜態(tài)文件
<!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"> 請(qǐng)選擇您要上傳的文件:<input type="file" name="myFile[]" /><br/> 請(qǐng)選擇您要上傳的文件:<input type="file" name="myFile[]" /><br/> 請(qǐng)選擇您要上傳的文件:<input type="file" name="myFile[]" /><br/> 請(qǐng)選擇您要上傳的文件:<input type="file" name="myFile[]" /><br/> <input type="submit" value="上傳"/> </form> </body> </html>
打印一下$_FILES
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
)
)
)
可以得到一個(gè)三維數(shù)組。
復(fù)雜是復(fù)雜了,但復(fù)雜的有規(guī)律,各項(xiàng)數(shù)值都在一起了,很方便我們?nèi)≈担。?/p>
所以先得到文件信息,變成單文件處理那種信息
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;
}
然后之前的那種exit錯(cuò)誤,就把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
//判斷錯(cuò)誤號(hào)
$res=array();
if($fileInfo['error']===UPLOAD_ERR_OK){
//檢測(cè)上傳得到小
if($fileInfo['size']>$maxSize){
$res['mes']=$fileInfo['name'].'上傳文件過(guò)大';
}
$ext=getExt($fileInfo['name']);
//檢測(cè)上傳文件的文件類型
if(!in_array($ext,$allowExt)){
$res['mes']=$fileInfo['name'].'非法文件類型';
}
//檢測(cè)是否是真實(shí)的圖片類型
if($flag){
if(!getimagesize($fileInfo['tmp_name'])){
$res['mes']=$fileInfo['name'].'不是真實(shí)圖片類型';
}
}
//檢測(cè)文件是否是通過(guò)HTTP POST上傳上來(lái)的
if(!is_uploaded_file($fileInfo['tmp_name'])){
$res['mes']=$fileInfo['name'].'文件不是通過(guò)HTTP POST方式上傳上來(lái)的';
}
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'] = '上傳文件超過(guò)了PHP配置文件中upload_max_filesize選項(xiàng)的值';
break;
case 2 :
$res['mes'] = '超過(guò)了表單MAX_FILE_SIZE限制的大小';
break;
case 3 :
$res['mes'] = '文件部分被上傳';
break;
case 4 :
$res['mes'] = '沒(méi)有選擇上傳文件';
break;
case 6 :
$res['mes'] = '沒(méi)有找到臨時(shí)目錄';
break;
case 7 :
case 8 :
$res['mes'] = '系統(tǒng)錯(cuò)誤';
break;
}
return $res;
}
}
里面封裝了兩個(gè)小的
function getExt($filename){
return strtolower(pathinfo($filename,PATHINFO_EXTENSION));
}
/**
* 產(chǎn)生唯一字符串
* @return string
*/
function getUniName(){
return md5(uniqid(microtime(true),true));
}
然后靜態(tài)中,用multiple屬性實(shí)現(xiàn)多個(gè)文件的輸入;
<!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">
請(qǐng)選擇您要上傳的文件:<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 '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);
這樣子的幾個(gè)文件,就實(shí)現(xiàn)比較強(qiáng)大的面向過(guò)程的上傳文件的功能(學(xué)的叫一個(gè)心酸。。。);
四、面向?qū)ο蟮奈募蟼?br />
<?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];
}
/**
* 檢測(cè)上傳文件是否出錯(cuò)
* @return boolean
*/
protected function checkError(){
if(!is_null($this->fileInfo)){
if($this->fileInfo['error']>0){
switch($this->fileInfo['error']){
case 1:
$this->error='超過(guò)了PHP配置文件中upload_max_filesize選項(xiàng)的值';
break;
case 2:
$this->error='超過(guò)了表單中MAX_FILE_SIZE設(shè)置的值';
break;
case 3:
$this->error='文件部分被上傳';
break;
case 4:
$this->error='沒(méi)有選擇上傳文件';
break;
case 6:
$this->error='沒(méi)有找到臨時(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;
}
}
/**
* 檢測(cè)上傳文件的大小
* @return boolean
*/
protected function checkSize(){
if($this->fileInfo['size']>$this->maxSize){
$this->error='上傳文件過(guò)大';
return false;
}
return true;
}
/**
* 檢測(cè)擴(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;
}
/**
* 檢測(cè)文件的類型
* @return boolean
*/
protected function checkMime(){
if(!in_array($this->fileInfo['type'],$this->allowMime)){
$this->error='不允許的文件類型';
return false;
}
return true;
}
/**
* 檢測(cè)是否是真實(shí)圖片
* @return boolean
*/
protected function checkTrueImg(){
if($this->imgFlag){
if(!@getimagesize($this->fileInfo['tmp_name'])){
$this->error='不是真實(shí)圖片';
return false;
}
return true;
}
}
/**
* 檢測(cè)是否通過(guò)HTTP POST方式上傳上來(lái)的
* @return boolean
*/
protected function checkHTTPPost(){
if(!is_uploaded_file($this->fileInfo['tmp_name'])){
$this->error='文件不是通過(guò)HTTP POST方式上傳上來(lái)的';
return false;
}
return true;
}
/**
*顯示錯(cuò)誤
*/
protected function showError(){
exit('<span style="color:red">'.$this->error.'</span>');
}
/**
* 檢測(cè)目錄不存在則創(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;
四、下載
對(duì)于瀏覽器不識(shí)別的,可以直接下載,但對(duì)于能識(shí)別的,需要多一兩步
<!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">通過(guò)程序下載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);
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- PHP中ajax無(wú)刷新上傳圖片與圖片下載功能
- php+ajax實(shí)現(xiàn)帶進(jìn)度條的上傳圖片功能【附demo源碼下載】
- PHP SFTP實(shí)現(xiàn)上傳下載功能
- php使用ftp實(shí)現(xiàn)文件上傳與下載功能
- PHP操作FTP類 (上傳、下載、移動(dòng)、創(chuàng)建等)
- PHP實(shí)現(xiàn)文件上傳與下載實(shí)例與總結(jié)
- php多文件上傳下載示例分享
- php上傳apk后自動(dòng)提取apk包信息的使用(示例下載)
- php下連接ftp實(shí)現(xiàn)文件的上傳、下載、刪除文件實(shí)例代碼
- 詳解PHP素材圖片上傳、下載功能
相關(guān)文章
Laravel框架環(huán)境與配置操作實(shí)例分析
這篇文章主要介紹了Laravel框架環(huán)境與配置操作,結(jié)合實(shí)例形式分析了laravel框架基本環(huán)境配置方法及維護(hù)模式相關(guān)操作技巧,需要的朋友可以參考下2019-12-12
windows環(huán)境下使用Composer安裝ThinkPHP5
本文給大家分享的是在windows環(huán)境下使用Composer安裝ThinkPHP5的具體步驟和方法,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下2018-05-05
Thinkphp5.0框架視圖view的循環(huán)標(biāo)簽用法示例
這篇文章主要介紹了Thinkphp5.0框架視圖view的循環(huán)標(biāo)簽用法,結(jié)合實(shí)例形式分析了thinkPHP5框架視圖view中的volist標(biāo)簽、foreach標(biāo)簽、for標(biāo)簽相關(guān)使用方法,需要的朋友可以參考下2019-10-10
Laravel 5框架學(xué)習(xí)之環(huán)境與配置
本文給大家主要介紹的是Laravel5框架中的環(huán)境配置,給大家詳細(xì)介紹了.env的配置文件,包含的數(shù)據(jù)庫(kù)配置信息的詳細(xì)解釋,這里推薦給大家,有需要的小伙伴參考下。2015-04-04
workerman結(jié)合laravel開(kāi)發(fā)在線聊天應(yīng)用的示例代碼
聊天功能是很常見(jiàn)的一種功能,Workerman是一款開(kāi)源高性能異步PHP socket即時(shí)通訊框架。這篇文章主要介紹了workerman結(jié)合laravel開(kāi)發(fā)在線聊天應(yīng)用,感興趣的小伙伴們可以參考一下2018-10-10
PHP二維數(shù)組排序的3種方法和自定義函數(shù)分享
這篇文章主要介紹了PHP二維數(shù)組排序的3種方法和自定義函數(shù)分享,需要的朋友可以參考下2014-04-04

