溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

利用Thinkphp怎么實(shí)現(xiàn)一個(gè)多文件上傳功能

發(fā)布時(shí)間:2020-12-24 15:45:42 來(lái)源:億速云 閱讀:207 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

本篇文章為大家展示了利用Thinkphp怎么實(shí)現(xiàn)一個(gè)多文件上傳功能,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

Thinkphp手冊(cè)中對(duì)于多文件上傳描述的很清楚:如果需要使用多個(gè)文件,只需要修改表單,把

復(fù)制代碼 代碼如下:

<input type='file' name='photo'>

改為

復(fù)制代碼 代碼如下:

<li><input type='file' name='photo1'></li>
<li><input type='file' name='photo2'></li>
<li><input type='file' name='photo3'></li>

或者

復(fù)制代碼 代碼如下:

<li><input type='file' name='photo[]'></li>
<li><input type='file' name='photo[]'></li>
<li><input type='file' name='photo[]'></li>

暫且自己的上傳表單域?yàn)閮蓚€(gè),一個(gè)上傳圖片,一個(gè)上傳視頻。字段名為image、video。
html代碼如下

復(fù)制代碼 代碼如下:

圖片:<input type="file" name="image[]">
 
視頻:<input type="file" name="video[]">


model代碼:

復(fù)制代碼 代碼如下:

protected $info= '';
 
protected $_auto = array(
array('image','upload',3,callback),//自動(dòng)完成方法
array('video','videoupload',3,callback), //自動(dòng)完成方法
);//自動(dòng)填充上傳圖片生成縮略圖
protected function upload(){
$var = $_FILES['image']['name'];
import('ORG.Net.UploadFile');
$upload = new UploadFile();
$upload->saveRule  = time;
$upload->allowExts  = array('jpg', 'gif', 'png', 'zip','flv');
$upload->thumb = true;
//視頻路徑。。。只支持flv后綴,
$upload->videopath = './Public/upload/Video/';
$upload->savePath =  './Public/upload/images/';
$upload->thumbPrefix = '250_115_,150_110_,213_156_';
$upload->thumbMaxWidth='250,150,213';
$upload->thumbMaxHeight='115,110,156';
if(!in_array('',$var) || !in_array('',$_FILES['video']['name'])){
if(!$upload->upload()) {
echo $upload->getErrorMsg();die;
}else{
$this->info =  $upload->getUploadFileInfo();
if(!in_array('',$var) &amp;&amp; !in_array('',$_FILES['video']['name'])){
return $this->info[1]['savename'];
}elseif(!in_array('',$var)){
return $this->info[0]['savename'];
}else{
return false;
}
 
}
}else{
return flase;
}
}
//上傳視頻
protected function videoupload(){
if(!in_array('',$var) &amp;&amp; !in_array('',$_FILES['video']['name'])){
return $this->info[0]['savename'];
}elseif(!in_array('',$_FILES['video']['name'])){
return $this->info[1]['savename'];
}else{
return false;
}
 
}


文章最后我來(lái)分析一下多文件上傳原理吧,先來(lái)看看html代碼

復(fù)制代碼 代碼如下:

<li><input type='file' name='photo[]'></li>
<li><input type='file' name='photo[]'></li>
<li><input type='file' name='photo[]'></li>


這種就是把表單變量定義為數(shù)組,在php中數(shù)組特殊變量它可以存儲(chǔ)多個(gè)不定長(zhǎng)的內(nèi)容,所以我們就可以自定多文件上傳框了,那么在php處理時(shí)我們要如何操作,下面看例子。

復(fù)制代碼 代碼如下:

protected $_auto = array(
array('image','upload',3,callback),//自動(dòng)完成方法
array('video','videoupload',3,callback), //自動(dòng)完成方法
);//自動(dòng)填充上傳圖片生成縮略圖


這個(gè)是告訴thinkphp是數(shù)組變量了,并不需要像原生態(tài)的php中來(lái)判斷遍歷數(shù)組長(zhǎng)度再一個(gè)個(gè)上傳的代碼了,因?yàn)榱藅hinkphp己經(jīng)做好了。

上述內(nèi)容就是利用Thinkphp怎么實(shí)現(xiàn)一個(gè)多文件上傳功能,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI