php layui实现前端多图上传实例

在本篇文章里小编给大家整理的是关于php结合layui前端实现多图上传的实例内容,有需要的朋友们可以参考下。

php结合layui前端实现多图上传

前端html代码

  1. <div class="layui-upload">
  2. <button type="button" class="layui-btn layui-btn-normal" >请选择图片</button>
  3. <span class="num_pic"></span>
  4. <div class="layui-upload-list">
  5. <table class="layui-table">
  6. <thead>
  7. <tr>
  8. <th>文件名</th>
  9. <th >图片预览</th>
  10. <th>大小</th>
  11. <th>状态</th>
  12. <th >操作</th>
  13. </tr>
  14. </thead>
  15. <tbody ></tbody>
  16. </table>
  17. </div>
  18. <button type="button" class="layui-btn" >开始上传</button>
  19. <span class="num_pic"></span>
  20. </div>

js 代码

  1. <script type="text/javascript">
  2. layui.use('upload', function() {
  3. var $ = layui.jquery,
  4. upload = layui.upload;
  5. //多文件列表示例
  6. var demoListView = $('#demoList'),
  7. uploadListIns = upload.render({
  8. elem: '#testList',
  9. url: "{url('pic/index/upload')}",
  10. accept: 'images',
  11. acceptMime: 'image/*',
  12. size: 8192,
  13. multiple: true,
  14. number: 400,
  15. auto: false,
  16. exts: 'jpg|png|jpeg',
  17. bindAction: '#testListAction',
  18. choose: function(obj) {
  19. var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
  20. //读取本地文件
  21. obj.preview(function(index, file, result) {
  22. var tr = $(['<tr + index + '">', '<td>' + file.name + '</td>', '<td><img src="' + result + '" alt="' + file.name + '" ></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(''));
  23. //单个重传
  24. tr.find('.demo-reload').on('click', function() {
  25. obj.upload(index, file);
  26. $("#upload-" + index).find("td").eq(2).html((file.size / 1014).toFixed(1) + 'kb');
  27. });
  28. //删除
  29. tr.find('.demo-delete').on('click', function() {
  30. delete files[index]; //删除对应的文件
  31. tr.remove();
  32. uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选
  33. });
  34. demoListView.append(tr);
  35. $(".num_pic").text("总共【" + demoListView.find("tr").length + "】张图片");
  36. });
  37. },
  38. done: function(res, index, upload) {
  39. if(res.code == 0) { //上传成功
  40. $("#cao").text("地址");
  41. var tr = demoListView.find('tr#upload-' + index),
  42. tds = tr.children();
  43. tds.eq(3).html('<span >上传成功</span>');
  44. tds.eq(4).html('<input type="text" name="imgs[]" value="' + res.file + '" class="layui-input" />'); //清空操作
  45. return delete this.files[index]; //删除文件队列已经上传成功的文件
  46. }
  47. this.error(index, upload);
  48. },
  49. allDone: function(obj) { //当文件全部被提交后,才触发
  50. layer.msg("上传文件数量:【" + obj.total + "】张,上传成功:【" + obj.successful + "】张,失败:【" + obj.aborted + "】", {
  51. time: 3000
  52. });
  53. console.log(obj.total); //得到总文件数
  54. console.log(obj.successful); //请求成功的文件数
  55. console.log(obj.aborted); //请求失败的文件数
  56. },
  57. error: function(index, upload) {
  58. var tr = demoListView.find('tr#upload-' + index),
  59. tds = tr.children();
  60. tds.eq(2).html('<span >上传失败</span>');
  61. tds.eq(4).find('.demo-reload').removeClass('layui-hide'); //显示重传
  62. }
  63. });
  64. });
  65. </script>

后端代码

  1. public function uploadAction(){
  2. $file=$_FILES['file'];
  3. $root_url = 'uploadfiles/pic/image/';
  4. if (!is_uploaded_file($file['tmp_name'])){
  5. $data = array('code'=>1,'msg'=>"错误");
  6. exit(json_encode($data,0));
  7. }
  8. /* $root_url.=date('Ymd').'/';*/
  9. $ext = pathinfo($file['name']);
  10. $num=makenum($this->memberinfo['id']);
  11. $root_url.=$num.'/';
  12. if (!is_dir($root_url)) {
  13. mkdir($root_url,0777, true);
  14. }
  15. $pa=file_list::get_file_list($root_url);
  16. $na=count($pa) + 1;
  17. if ($na<10){
  18. $name=$num.'-000'.$na;
  19. }elseif($na<100){
  20. $name=$num.'-00'.$na;
  21. }elseif($na<1000){
  22. $name=$num.'-0'.$na;
  23. }else{
  24. $name=$num.'-'.$na;
  25. }
  26. $n=$root_url.$name.".".$ext['extension'];
  27. $result=move_uploaded_file($file['tmp_name'],$n);
  28. if ($result){
  29. exit(json_encode(array("code"=>0,"msg"=>"ok","file"=>$n,"size"=>$file['size']),0));
  30. }else{
  31. exit(json_encode(array("code"=>1,"msg"=>"false","file"=>$n,"size"=>$file['size']),0));
  32. }
  33. }

上传效果:

php layui实现前端多图上传实例