AssetController.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: kane <chengjin005@163.com>
  10. // +----------------------------------------------------------------------
  11. namespace admin\user\controller;
  12. use cmf\controller\AdminBaseController;
  13. use cmf\lib\Upload;
  14. use think\Lang;
  15. /**
  16. * 附件上传控制器
  17. * Class Asset
  18. * @package app\asset\controller
  19. */
  20. class AssetController extends AdminBaseController
  21. {
  22. public function _initialize()
  23. {
  24. $adminId = cmf_get_current_admin_id();
  25. $userId = cmf_get_current_user_id();
  26. if (empty($adminId) && empty($userId)) {
  27. exit("非法上传!");
  28. }
  29. Lang::load(APP_PATH.'user/lang'.DS.$this->lang.DS.'admin_webuploader'.EXT);
  30. }
  31. /**
  32. * webuploader 上传
  33. */
  34. public function webuploader()
  35. {
  36. if ($this->request->isPost()) {
  37. $uploader = new Upload();
  38. $result = $uploader->upload();
  39. if ($result === false) {
  40. $this->adminError($uploader->getError());
  41. } else {
  42. $this->adminSuccess("上传成功!", '', $result);
  43. }
  44. } else {
  45. $uploadSetting = cmf_get_upload_setting();
  46. $arrFileTypes = [
  47. 'image' => ['title' => 'Image files', 'extensions' => $uploadSetting['file_types']['image']['extensions']],
  48. 'video' => ['title' => 'Video files', 'extensions' => $uploadSetting['file_types']['video']['extensions']],
  49. 'audio' => ['title' => 'Audio files', 'extensions' => $uploadSetting['file_types']['audio']['extensions']],
  50. 'file' => ['title' => 'Custom files', 'extensions' => $uploadSetting['file_types']['file']['extensions']]
  51. ];
  52. $arrData = $this->request->param();
  53. if (empty($arrData["filetype"])) {
  54. $arrData["filetype"] = "image";
  55. }
  56. $fileType = $arrData["filetype"];
  57. if (array_key_exists($arrData["filetype"], $arrFileTypes)) {
  58. $extensions = $uploadSetting['file_types'][$arrData["filetype"]]['extensions'];
  59. $fileTypeUploadMaxFileSize = $uploadSetting['file_types'][$fileType]['upload_max_filesize'];
  60. } else {
  61. $this->adminError('上传文件类型配置错误!');
  62. }
  63. $this->assign('filetype', $arrData["filetype"]);
  64. $this->assign('extensions', $extensions);
  65. $this->assign('upload_max_filesize', $fileTypeUploadMaxFileSize * 1024);
  66. $this->assign('upload_max_filesize_mb', intval($fileTypeUploadMaxFileSize / 1024));
  67. $maxFiles = intval($uploadSetting['max_files']);
  68. $maxFiles = empty($maxFiles) ? 20 : $maxFiles;
  69. $chunkSize = intval($uploadSetting['chunk_size']);
  70. $chunkSize = empty($chunkSize) ? 512 : $chunkSize;
  71. $this->assign('max_files', $arrData["multi"] ? $maxFiles : 1);
  72. $this->assign('chunk_size', $chunkSize); //// 单位KB
  73. $this->assign('multi', $arrData["multi"]);
  74. $this->assign('app', $arrData["app"]);
  75. return $this->fetch(":webuploader");
  76. }
  77. }
  78. }