action_upload.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. include "Uploader.class.php";
  3. /* 上传配置 */
  4. $base64 = "upload";
  5. switch (htmlspecialchars($_GET['action'])) {
  6. case 'uploadimage':
  7. $config = array(
  8. "pathFormat" => $CONFIG['imagePathFormat'],
  9. "maxSize" => $CONFIG['imageMaxSize'],
  10. "allowFiles" => $CONFIG['imageAllowFiles']
  11. );
  12. $fieldName = $CONFIG['imageFieldName'];
  13. break;
  14. case 'uploadscrawl':
  15. $config = array(
  16. "pathFormat" => $CONFIG['scrawlPathFormat'],
  17. "maxSize" => $CONFIG['scrawlMaxSize'],
  18. "allowFiles" => $CONFIG['scrawlAllowFiles'],
  19. "oriName" => "scrawl.png"
  20. );
  21. $fieldName = $CONFIG['scrawlFieldName'];
  22. $base64 = "base64";
  23. break;
  24. case 'uploadvideo':
  25. $config = array(
  26. "pathFormat" => $CONFIG['videoPathFormat'],
  27. "maxSize" => $CONFIG['videoMaxSize'],
  28. "allowFiles" => $CONFIG['videoAllowFiles']
  29. );
  30. $fieldName = $CONFIG['videoFieldName'];
  31. break;
  32. case 'uploadfile':
  33. default:
  34. $config = array(
  35. "pathFormat" => $CONFIG['filePathFormat'],
  36. "maxSize" => $CONFIG['fileMaxSize'],
  37. "allowFiles" => $CONFIG['fileAllowFiles']
  38. );
  39. $fieldName = $CONFIG['fileFieldName'];
  40. break;
  41. }
  42. /* 生成上传实例对象并完成上传 */
  43. $up = new Uploader($fieldName, $config, $base64);
  44. /**
  45. * 得到上传文件所对应的各个参数,数组结构
  46. * array(
  47. * "state" => "", //上传状态,上传成功时必须返回"SUCCESS"
  48. * "url" => "", //返回的地址
  49. * "title" => "", //新文件名
  50. * "original" => "", //原始文件名
  51. * "type" => "" //文件类型
  52. * "size" => "", //文件大小
  53. * )
  54. */
  55. $result = $up->getFileInfo();
  56. $fileUrl = $result['url'];
  57. $filePath = ltrim(str_replace(UPLOAD_URL,'',$fileUrl),'/');//文件相对路径
  58. $targetFile = str_replace('/',DIRECTORY_SEPARATOR,UPLOAD_PATH.$filePath);
  59. $filesize = filesize($targetFile);
  60. //上传设置(判断是否采用全路径)
  61. $attConfigInfo = \app\modules\admin\models\Config::find()->where("name='attachment'")->one();
  62. $attConfig = string2array($attConfigInfo->value);
  63. if(Yii::$app->params['oss']['OPEN_OSS']==1)//如果开启了存储
  64. {
  65. $oss = new \app\common\components\Oss();
  66. $initResult = $oss->init();
  67. ob_clean();
  68. if($initResult['error']==0)
  69. {
  70. $ossResult = $oss->Upload($targetFile,$filePath);
  71. if($ossResult['error']==0)
  72. {
  73. $fileUrl = $ossResult['data']['url'];
  74. $filePath = $attConfig['absolute_url']?$ossResult['data']['url']:$ossResult['data']['path'];
  75. @unlink($targetFile);
  76. $result['url'] = getFileUrl($filePath);
  77. }
  78. else
  79. {
  80. return json_encode([
  81. 'code' => 1,
  82. 'msg' => $ossResult['msg']
  83. ]);
  84. }
  85. }
  86. else
  87. {
  88. return json_encode([
  89. 'code' => 1,
  90. 'msg' => $initResult['msg']
  91. ]);
  92. }
  93. }
  94. else
  95. {
  96. $filePath = $attConfig['absolute_url']?$fileUrl:$filePath;
  97. }
  98. //写入附件表
  99. $attachment = new \app\modules\admin\models\Attachment();
  100. $attachment->file_name = basename($filePath);
  101. $attachment->file_path = $filePath;
  102. $attachment->file_size = $filesize;
  103. $attachment->file_ext = fileext(basename($filePath));
  104. $attachment->hash = md5($filePath);
  105. $attachment->upload_time = time();
  106. $attachment->upload_ip = ip();
  107. $attachment->status = 1;
  108. $attachment->list_order = 0;
  109. $attachment->folder = Yii::$app->request->post('folder','');
  110. $attachment->save();
  111. return json_encode($result);