前端html程式碼
<div class="layui-upload"> <button type="button" class="layui-btn layui-btn-normal" id="testList">請選擇圖片</button> <span class="num_pic"></span> <div class="layui-upload-list"> <table class="layui-table"> <thead> <tr> <th>檔名</th> <th id="pic">圖片預覽</th> <th>大小</th> <th>狀態</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(); //將每次選擇的檔案追加到檔案佇列 //讀取本地檔案 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('')); //單個重傳 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]; //刪除對應的檔案 tr.remove(); uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免刪除後出現同名檔案不可選 }); 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]; //刪除檔案佇列已經上傳成功的檔案 } this.error(index, upload); }, allDone: function(obj) { //當檔案全部被提交後,才觸發 layer.msg("上傳檔案數量:【" + obj.total + "】張,上傳成功:【" + obj.successful + "】張,失敗:【" + obj.aborted + "】", { time: 3000 }); console.log(obj.total); //得到總檔案數 console.log(obj.successful); //請求成功的檔案數 console.log(obj.aborted); //請求失敗的檔案數 }, 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'=>"錯誤"); 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結合layui前端實現多圖上傳的詳細內容,更多請關注TW511.COM其它相關文章!