php layui實(shí)現(xiàn)前端多圖上傳實(shí)例
php結(jié)合layui前端實(shí)現(xiàn)多圖上傳
前端html代碼
<div class="layui-upload"> <button type="button" class="layui-btn layui-btn-normal" id="testList">請(qǐng)選擇圖片</button> <span class="num_pic"></span> <div class="layui-upload-list"> <table class="layui-table"> <thead> <tr> <th>文件名</th> <th id="pic">圖片預(yù)覽</th> <th>大小</th> <th>狀態(tài)</th> <th id="cao">操作</th> </tr> </thead> <tbody id="demoList"></tbody> </table> </div> <button type="button" class="layui-btn" id="testListAction">開(kāi)始上傳</button> <span class="num_pic"></span> </div>
js 代碼
<script type="text/javascript"> layui.use('upload', function() { var $ = layui.jquery, upload = layui.upload; //多文件列表示例 var demoListView = $('#demoList'), uploadListIns = upload.render({ elem: '#testList', url: "{url('pic/index/upload')}", accept: 'images', acceptMime: 'image/*', size: 8192, multiple: true, number: 400, auto: false, exts: 'jpg|png|jpeg', bindAction: '#testListAction', choose: function(obj) { var files = this.files = obj.pushFile(); //將每次選擇的文件追加到文件隊(duì)列 //讀取本地文件 obj.preview(function(index, file, result) { var tr = $(['<tr id="upload-' + index + '">', '<td>' + file.name + '</td>', '<td><img src="' + result + '" alt="' + file.name + '" style="width: 100px;height: 40px;"></td>', '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>', '<td>等待上傳</td>', '<td>', '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重傳</button>', '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">刪除</button>', '</td>', '</tr>'].join('')); //單個(gè)重傳 tr.find('.demo-reload').on('click', function() { obj.upload(index, file); $("#upload-" + index).find("td").eq(2).html((file.size / 1014).toFixed(1) + 'kb'); }); //刪除 tr.find('.demo-delete').on('click', function() { delete files[index]; //刪除對(duì)應(yīng)的文件 tr.remove(); uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免刪除后出現(xiàn)同名文件不可選 }); demoListView.append(tr); $(".num_pic").text("總共【" + demoListView.find("tr").length + "】張圖片"); }); }, done: function(res, index, upload) { if(res.code == 0) { //上傳成功 $("#cao").text("地址"); var tr = demoListView.find('tr#upload-' + index), tds = tr.children(); tds.eq(3).html('<span style="color: #5FB878;">上傳成功</span>'); tds.eq(4).html('<input type="text" name="imgs[]" value="' + res.file + '" class="layui-input" />'); //清空操作 return delete this.files[index]; //刪除文件隊(duì)列已經(jīng)上傳成功的文件 } this.error(index, upload); }, allDone: function(obj) { //當(dāng)文件全部被提交后,才觸發(fā) layer.msg("上傳文件數(shù)量:【" + obj.total + "】張,上傳成功:【" + obj.successful + "】張,失?。骸? + obj.aborted + "】", { time: 3000 }); console.log(obj.total); //得到總文件數(shù) console.log(obj.successful); //請(qǐng)求成功的文件數(shù) console.log(obj.aborted); //請(qǐng)求失敗的文件數(shù) }, error: function(index, upload) { var tr = demoListView.find('tr#upload-' + index), tds = tr.children(); tds.eq(2).html('<span style="color: #FF5722;">上傳失敗</span>'); tds.eq(4).find('.demo-reload').removeClass('layui-hide'); //顯示重傳 } }); }); </script>
后端代碼
public function uploadAction(){ $file=$_FILES['file']; $root_url = 'uploadfiles/pic/image/'; if (!is_uploaded_file($file['tmp_name'])){ $data = array('code'=>1,'msg'=>"錯(cuò)誤"); exit(json_encode($data,0)); } /* $root_url.=date('Ymd').'/';*/ $ext = pathinfo($file['name']); $num=makenum($this->memberinfo['id']); $root_url.=$num.'/'; if (!is_dir($root_url)) { mkdir($root_url,0777, true); } $pa=file_list::get_file_list($root_url); $na=count($pa) + 1; if ($na<10){ $name=$num.'-000'.$na; }elseif($na<100){ $name=$num.'-00'.$na; }elseif($na<1000){ $name=$num.'-0'.$na; }else{ $name=$num.'-'.$na; } $n=$root_url.$name.".".$ext['extension']; $result=move_uploaded_file($file['tmp_name'],$n); if ($result){ exit(json_encode(array("code"=>0,"msg"=>"ok","file"=>$n,"size"=>$file['size']),0)); }else{ exit(json_encode(array("code"=>1,"msg"=>"false","file"=>$n,"size"=>$file['size']),0)); } }
上傳效果:
以上就是php結(jié)合layui前端實(shí)現(xiàn)多圖上傳的全部知識(shí)點(diǎn),感謝大家對(duì)腳本之家的支持。
相關(guān)文章
php版微信公眾平臺(tái)開(kāi)發(fā)之驗(yàn)證步驟實(shí)例詳解
這篇文章主要介紹了php版微信公眾平臺(tái)開(kāi)發(fā)之驗(yàn)證步驟,結(jié)合實(shí)例形式詳細(xì)分析了php微信公眾平臺(tái)驗(yàn)證的操作步驟與相關(guān)參數(shù)含義,需要的朋友可以參考下2016-09-09PHP實(shí)現(xiàn)登錄驗(yàn)證碼校驗(yàn)功能
這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)驗(yàn)證碼校驗(yàn)功能,主要是利用PHP中的 SESSION功能來(lái)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05php大小寫(xiě)轉(zhuǎn)換函數(shù)(strtolower、strtoupper)用法介紹
strtoupper() 函數(shù)把字符串轉(zhuǎn)換為大寫(xiě)而strtolower函數(shù): 該函數(shù)將傳入的字符串參數(shù)所有的字符都轉(zhuǎn)換成小寫(xiě),并以小定形式放回這個(gè)字符串。下面通過(guò)本文給大家分享php大小寫(xiě)轉(zhuǎn)換函數(shù)(strtolower、strtoupper)用法,需要的朋友參考下吧2017-11-11在win7中搭建Linux+PHP 開(kāi)發(fā)環(huán)境
這篇文章會(huì)以一個(gè)概覽的形式介紹我的 PHP 開(kāi)發(fā)環(huán)境,列出我使用的工具,但配置過(guò)程不會(huì)太詳細(xì)。有需要的朋友可以參考下。2014-10-10PHP7使用ODBC連接SQL Server2008 R2數(shù)據(jù)庫(kù)示例【基于thinkPHP5.1框架】
這篇文章主要介紹了PHP7使用ODBC連接SQL Server2008 R2數(shù)據(jù)庫(kù),結(jié)合實(shí)例形式分析了基于thinkPHP5.1框架使用ODBC連接SQL Server2008數(shù)據(jù)庫(kù)相關(guān)操作技巧,需要的朋友可以參考下2019-05-05Yii1.1框架實(shí)現(xiàn)PHP極光推送消息通知功能
這篇文章主要介紹了Yii1.1框架實(shí)現(xiàn)PHP極光推送消息通知的相關(guān)資料,需要的朋友可以參考下2018-09-09PHPCMS手機(jī)站偽靜態(tài)設(shè)置詳細(xì)教程
這篇文章主要介紹了PHPCMS手機(jī)站偽靜態(tài)設(shè)置教程,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02