您好,登錄后才能下訂單哦!
這篇文章主要介紹了php結(jié)合layui前端實(shí)現(xiàn)多圖上傳的方法,具有一定借鑒價(jià)值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
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">開始上傳</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 + "】張,失?。骸?quot; + 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)); } }
上傳效果:
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享php結(jié)合layui前端實(shí)現(xiàn)多圖上傳的方法內(nèi)容對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細(xì)的解決方法等著你來學(xué)習(xí)!
免責(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)容。