TinymceController.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Filesystem\FilesystemAdapter;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Storage;
  6. use Symfony\Component\HttpFoundation\File\UploadedFile;
  7. class TinymceController
  8. {
  9. public function upload(Request $request)
  10. {
  11. $file = $request->file('file');
  12. // $dir = trim($request->get('dir'), '/');
  13. $disk = $this->disk();
  14. //定死上传目录
  15. $dir = config("admin.upload.tinymce_directory.image").'/'.date('Ymd');
  16. if (empty($dir)) {
  17. die('dir error');
  18. }
  19. //定死上传目录 end
  20. $newName = $this->generateNewName($file);
  21. $disk->putFileAs($dir, $file, $newName);
  22. return ['location' => $disk->url("{$dir}/$newName")];
  23. }
  24. protected function generateNewName(UploadedFile $file)
  25. {
  26. return uniqid(md5($file->getClientOriginalName())).'.'.$file->getClientOriginalExtension();
  27. }
  28. /**
  29. * @return \Illuminate\Contracts\Filesystem\Filesystem|FilesystemAdapter
  30. */
  31. protected function disk()
  32. {
  33. $disk = request()->get('disk') ?: config('admin.upload.disk');
  34. return Storage::disk($disk);
  35. }
  36. }