UploadController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Http\Controllers\Api;
  12. use Illuminate\Http\Request;
  13. /**
  14. * 上传文件-控制器
  15. * @since 2020/11/11
  16. * Class UploadController
  17. * @package App\Http\Controllers
  18. */
  19. class UploadController extends webApp
  20. {
  21. /**
  22. * 上传图片
  23. * @param Request $request 网络请求
  24. * @return array
  25. * @since 2020/11/11
  26. */
  27. public function uploadImage(Request $request)
  28. {
  29. // 上传单图统一调取方法
  30. $result = upload_image($request, 'file', '',$this->userId);
  31. if (!$result['success']) {
  32. return showJson($result['msg'], false, '');
  33. }
  34. // 文件路径
  35. $file_path = $result['data']['img_path'];
  36. if (!$file_path) {
  37. return showJson('文件上传失败', false, '');
  38. }
  39. // 网络域名拼接
  40. if ($file_path && strpos($file_path, IMG_URL) === false) {
  41. $file_path = get_image_url($file_path, false);
  42. }
  43. // 返回结果
  44. return showJson('上传成功', true, ['url'=> $file_path, 'path'=> $result['data']['img_path']]);
  45. }
  46. /**
  47. * 上传文件(单个上传)
  48. * @param Request $request
  49. * @since 2020/11/11
  50. */
  51. public function uploadFile(Request $request)
  52. {
  53. $result = upload_file($request,'file','', $this->userId);
  54. if (!$result['success']) {
  55. return showJson($result['msg'], false, '');
  56. }
  57. // 文件路径
  58. $file_path = isset($result['data']['file_path'])? $result['data']['file_path'] : '';
  59. if (!$file_path) {
  60. return showJson("文件上传失败",false);
  61. }
  62. // 网络域名拼接
  63. if (strpos($file_path, IMG_URL) === false) {
  64. $file_path = get_image_url($file_path, false);
  65. }
  66. // 返回结果
  67. return showJson(MESSAGE_OK, true, ['url'=> $file_path, 'path'=> $result['data']['file_path']]);
  68. }
  69. /**
  70. * 上传视频
  71. * @param Request $request
  72. * @since 2020/11/11
  73. */
  74. public function uploadVideo(Request $request)
  75. {
  76. $result = upload_video($request,'file','', $this->userId);
  77. if (!$result['success']) {
  78. return showJson($result['msg'], false, '');
  79. }
  80. // 文件路径
  81. $file_path = isset($result['data']['file_path'])? $result['data']['file_path'] : '';
  82. if (!$file_path) {
  83. return showJson("视频上传失败",false);
  84. }
  85. // 网络域名拼接
  86. if (strpos($file_path, IMG_URL) === false) {
  87. $file_path = get_image_url($file_path, false);
  88. }
  89. // 返回结果
  90. return showJson(MESSAGE_OK, true, ['url'=> $file_path, 'path'=> $result['data']['file_path']]);
  91. }
  92. /**
  93. * @return array
  94. */
  95. public function deleteImage()
  96. {
  97. $url = request()->post('url','');
  98. if($url){
  99. $path = get_image_path($url);
  100. if(file_exists(ATTACHMENT_PATH.$path)){
  101. @unlink(ATTACHMENT_PATH.$path);
  102. }
  103. if(file_exists(ATTACHMENT_PATH.$path)){
  104. @unlink(ATTACHMENT_PATH.$path);
  105. }
  106. }
  107. return showJson('操作成功',true);
  108. }
  109. }