UploadController.class.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace Admin\Controller;
  3. use Think\Page;
  4. /**
  5. * 后台上传控制器
  6. *
  7. */
  8. class UploadController extends AdminController
  9. {
  10. /**
  11. * 上传列表
  12. *
  13. */
  14. public function index()
  15. {
  16. //搜索
  17. $keyword = I('keyword', '', 'string');
  18. $condition = array('like', '%' . $keyword . '%');
  19. $map['id|path'] = array($condition, $condition, '_multi' => true);
  20. //获取所有上传
  21. $map['status'] = array('egt', '0'); //禁用和正常状态
  22. $p = !empty($_GET["p"]) ? $_GET['p'] : 1;
  23. $upload_object = D('Upload');
  24. $data_list = $upload_object
  25. ->page($p, C('ADMIN_PAGE_ROWS'))
  26. ->where($map)
  27. ->order('sort desc,id desc')
  28. ->select();
  29. $page = new Page(
  30. $upload_object->where($map)->count(),
  31. C('ADMIN_PAGE_ROWS')
  32. );
  33. foreach ($data_list as &$data) {
  34. $data['name'] = cut_str($data['name'], 0, 30)
  35. . '<input class="form-control input-sm" value="'
  36. . $data['path'] . '">';
  37. }
  38. // 使用Builder快速建立列表页面。
  39. $builder = new \Common\Builder\ListBuilder();
  40. $builder->setMetaTitle('图片管理')// 设置页面标题
  41. ->addTopButton('resume')// 添加启用按钮
  42. ->addTopButton('forbid')// 添加禁用按钮
  43. ->addTopButton('delete')// 添加删除按钮
  44. ->setSearch('请输入ID/上传关键字', U('index'))
  45. ->addTableColumn('id', 'ID')
  46. ->addTableColumn('show', '文件')
  47. ->addTableColumn('name', '文件名及路径')
  48. ->addTableColumn('size', '大小', 'byte')
  49. ->addTableColumn('create_time', '创建时间', 'time')
  50. ->addTableColumn('sort', '排序')
  51. ->addTableColumn('status', '状态', 'status')
  52. ->addTableColumn('right_button', '操作', 'btn')
  53. ->setTableDataList($data_list)// 数据列表
  54. ->setTableDataPage($page->show())// 数据列表分页
  55. ->addRightButton('forbid')// 添加禁用/启用按钮
  56. ->addRightButton('delete')// 添加删除按钮
  57. ->display();
  58. }
  59. /**
  60. * 设置一条或者多条数据的状态
  61. *
  62. */
  63. public function setStatus($model = CONTROLLER_NAME, $script = false)
  64. {
  65. $ids = I('request.ids');
  66. $status = I('request.status');
  67. if (empty($ids)) {
  68. $this->error('请选择要操作的数据');
  69. }
  70. switch ($status) {
  71. case 'delete': // 删除条目
  72. if (!is_array($ids)) {
  73. $id_list[0] = $ids;
  74. } else {
  75. $id_list = $ids;
  76. }
  77. foreach ($id_list as $id) {
  78. $upload_info = D('Upload')->find($id);
  79. if ($upload_info) {
  80. $realpath = realpath('.' . $upload_info['path']);
  81. if ($realpath) {
  82. array_map("unlink", glob($realpath));
  83. if (count(glob($realpath))) {
  84. $this->error('删除失败!');
  85. } else {
  86. $resut = D('Upload')->delete($id);
  87. $this->success('删除成功!');
  88. }
  89. } else {
  90. $resut = D('Upload')->delete($id);
  91. $this->success('删除成功!');
  92. }
  93. }
  94. }
  95. break;
  96. default:
  97. parent::setStatus($model);
  98. break;
  99. }
  100. }
  101. /**
  102. * 上传
  103. *
  104. */
  105. public function upload()
  106. {
  107. $data = json_encode(D('Upload')->upload());
  108. exit($data);
  109. }
  110. /**
  111. * 下载
  112. *
  113. */
  114. public
  115. function download($token)
  116. {
  117. if (empty($token)) {
  118. $this->error('token参数错误!');
  119. }
  120. //解密下载token
  121. $file_md5 = \Think\Crypt::decrypt($token, user_md5(is_login()));
  122. if (!$file_md5) {
  123. $this->error('下载链接已过期,请刷新页面!');
  124. }
  125. $upload_object = D('Upload');
  126. $file_id = $upload_object->getFieldByMd5($file_md5, 'id');
  127. if (!$upload_object->download($file_id)) {
  128. $this->error($upload_object->getError());
  129. }
  130. }
  131. }