使用bootstrap外掛上傳檔案,檔案上傳樣式超級好看
阿新 • • 發佈:2019-02-16
最近需要做一個視訊上傳的功能,但是感覺平常的檔案上傳樣式不怎麼好看,於是就選擇了bootstrap的檔案上傳樣式,研究了研究,感覺還不錯,首先使用bootstrap應該下載bootatrap外掛。
下載地址:https://v2.bootcss.com/customize.html#download,
先看一下效果吧:
放上檔案或者視訊。可以實時觀看聽聲音
上傳成功之後返回done,下面是效果:
下面我們應該引入幾個bootstrap的js與css檔案,然後就是寫檔案上傳的form表單了
<link href="/backend/css/bootstrap.min.css" rel="stylesheet"><link type="text/css" rel="stylesheet" href="/backend/bootstrap-fileinput-master/css/fileinput.css" /> <script type="text/javascript" src="/backend/js/bootstrap.min.js"></script> <script type="text/javascript" src="/backend/js/jquery.min.js"></script> <script type="text/javascript"src="/backend/bootstrap-fileinput-master/js/fileinput.js"></script> <!--中文包--> <script type="text/javascript" src="/backend/bootstrap-fileinput-master/js/locales/zh.js"></script>
<div style="width: 300px; height: 300px;"> <form enctype="multipart/form-data"> <inputid="file-1" name="file" type="file" multiple class="file" data-overwrite-initial="false" data-min-file-count="1"/> </form> </div>
script程式碼如下:
<script> $("#file-1").fileinput({ uploadUrl: '/course/venderadd', // 必須設定個路徑進入php程式碼部分 allowedFileExtensions : ['jpg', 'png','gif','txt','zip','ico','jpeg','js','css','java','mp3','mp4','doc','docx'],//允許的檔案型別 overwriteInitial: false, maxFileSize: 600000,//檔案的最大大小 單位是k maxFilesNum: 10,//最多檔案數量 dataType:'json', // allowedFileTypes: ['image', 'video', 'flash'], slugCallback: function(filename) { return filename; } }); </script>
然後上傳檔案的名字到了venderadd的操作名裡面,php程式碼進行如下編寫:
#視訊上傳 public function actionVenderadd(){ $file=$_FILES['file'];//獲取上稱檔案的資訊,陣列形式 $Up_obj = new Upload; $path = $Up_obj->up($file); $root = Yii::$app ->request->hostInfo;//獲取當前訪問的地址 $new_path = $root . '/' .$path;//檔案上傳成功之後的路徑}
這裡我例項化了一個我的檔案上傳類進行上傳,檔案上傳類程式碼如下:
<?php
namespace yii\web;
header("content-type:text/html;charset=utf8");
/**
* Class Upload
* 檔案上傳
*/
class Upload{
public $max; //檔案大小public $arr=array("gif","png","jpg","bmp","jpeg",'mp4'); //檔案字尾
public $error; //檔案錯誤提示
/**
* File
* $file | 檔名
* return | 路徑 / false
*/
function up($file){
//驗證錯誤型別
switch ($file['error']) {
case '1': $this->error="上傳檔案超過php.ini中的設定";return false;
case '2': $this->error="上傳檔案超過表單中的設定";return flase;
case '3': $this->error="部分檔案被上傳";return false;
case '4': $this->error="沒有檔案被上傳";return false;
}
//檔案大小
$this->max=1024*1024*100;
if($file['size']>$this->max){
$this->error="上傳圖片超限";
return false;
}
//檔案型別
$style=substr($file['type'],strpos($file['type'],'/')+1);
if(!in_array($style,$this->arr)){
$this->error="上傳圖片型別錯誤";
return false;
}
//定義要上傳的路徑
$puth='Uploads'.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
//判斷路徑是否存在
is_dir($puth) or mkdir($puth,0777,true);
//定義新的檔名(1)擷取字尾名
$name=substr($file['name'],strpos($file['name'],'.'));
//(2)當前時間加隨機數命名檔案
$new_name=time().rand(10000,99999).$name;
//拼接要上傳的路徑
$new_puth=$puth.$new_name;
//移動檔案到到指定資料夾
if(!empty($new_puth)){
//把臨時路徑放到指定路徑
move_uploaded_file($file['tmp_name'], $new_puth);
return $new_puth;
}
}
/**
* File
* return | 錯誤詳情
*/
function getError(){
return $this->error;
}
}
?>
有什麼地方需要改進的還請各位大神指教!