FileService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace app\common\service;
  3. use think\File;
  4. use think\facade\Request;
  5. use think\facade\Validate;
  6. use think\facade\Filesystem;
  7. use app\common\service\Service;
  8. use Workerman\Worker;
  9. class FileService extends Service
  10. {
  11. /**
  12. * 上传图片
  13. * 本地运行如果无法写入图片可能是没有给public目录写权限
  14. *
  15. * @param string $path 上传路径,默认为'default'
  16. * @return array 上传后的图片路径
  17. */
  18. public function uploadImage($path = 'default')
  19. {
  20. // 获取表单上传文件
  21. $files = request()->file();
  22. // 验证上传文件格式和大小
  23. $this->validate($files, Validate::rule(['file' => 'fileSize:2048000|fileExt:jpeg,jpg,png,webp']));
  24. $file = request()->file('file');
  25. try {
  26. // 保存上传文件到public目录下的$path目录中
  27. $path = Filesystem::disk('public')->putFile($path, $file);
  28. } catch (\Exception $e) {
  29. throw $this->warpException($e, '上传图片失败:\n');
  30. }
  31. // 获取图片的url地址
  32. $path = Request::domain() . getVirRootDir() . '/storage/' . $path;
  33. return [
  34. 'path' => $path
  35. ];
  36. }
  37. /**
  38. * 上传附件
  39. * 本地运行如果无法写入图片可能是没有给public目录写权限
  40. *
  41. * @param string $path 上传路径,默认为'default'
  42. * @return array 上传后的图片路径
  43. */
  44. public function uploadAttachment($path = 'default')
  45. {
  46. // 获取表单上传文件
  47. if (count(Worker::getAllWorkers()) > 0) {
  48. $file = $_FILES[0]['file_data'];
  49. //$tmp_file = tempnam('','tm_'); 这种写法最终保存时扩展名为.tmp
  50. $tmp_file = sys_get_temp_dir() . '/' . uniqid() . '.' . explode('/', $_FILES[0]['file_type'])[1];
  51. file_put_contents($tmp_file, $file);
  52. $file = new File($tmp_file);
  53. // 验证上传文件格式和大小
  54. $files = ['file' => $file];
  55. if (
  56. !in_array($file->getMime(), [
  57. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', // docx
  58. 'application/pdf',
  59. 'application/msword' //doc
  60. ])
  61. ) {
  62. throw $this->exception('文件mime不符合要求,(请使用pdf,doc,docx)', 777);
  63. }
  64. } else {
  65. // 获取表单上传文件
  66. $files = request()->file();
  67. $file = request()->file('file');
  68. $this->validate($files, Validate::rule(['file' => 'fileExt:pdf,doc,docx']));
  69. }
  70. try {
  71. // 保存上传文件到public目录下的$path目录中
  72. $path = Filesystem::disk('public')->putFile($path, $file);
  73. } catch (\Exception $e) {
  74. throw $this->warpException($e, '上传图片失败:\n');
  75. }
  76. // 获取图片的url地址
  77. $path = Request::domain() . getVirRootDir() . '/storage/' . $path;
  78. return [
  79. 'path' => $path
  80. ];
  81. }
  82. /**
  83. * 获取当前网站的域名地址
  84. *
  85. * @return string 域名地址
  86. */
  87. protected static function get_domain()
  88. {
  89. $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
  90. return $sys_protocal . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
  91. }
  92. }