VideoUpload.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Widget\Tools;
  3. use App\Models\BaseAttachmentModel;
  4. use Illuminate\Support\Facades\Request;
  5. use Intervention\Image\Facades\Image;
  6. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  7. //use FFMpeg;
  8. /**
  9. * Created by PhpStorm.
  10. * User: steven
  11. * Date: 16/9/12
  12. * Time: 下午8:06
  13. */
  14. class VideoUpload
  15. {
  16. protected static function mkFolder($path)
  17. {
  18. if (!is_readable($path)) {
  19. mkdir($path, 0700, true);
  20. }
  21. }
  22. /*
  23. * 文件存储工具
  24. *
  25. */
  26. public static function mvFile($fileName)
  27. {
  28. $rel_path = '/upload/video/' . date('Ymd');
  29. $path = public_path() . $rel_path;
  30. if (!Request::hasFile($fileName)) return false;
  31. $file = Request::file($fileName);
  32. $fileSize = $file->getSize();
  33. $clientName = $file->getClientOriginalName();
  34. $md5 = md5($clientName . time());
  35. $md5_filename = $md5 . '.' . $file->getClientOriginalExtension();
  36. $fileMimeType = $file->getMimeType();
  37. try {
  38. if(!$file->move($path, $md5_filename)){
  39. return false;
  40. }
  41. $real_path = $path . '/' . $md5_filename;
  42. $url_path = $rel_path . '/' . $md5_filename;
  43. $attachment = new BaseAttachmentModel();
  44. $attachment->name = $clientName;
  45. $attachment->md5 = $md5;
  46. $attachment->path = $real_path;
  47. $attachment->url = $url_path;
  48. $attachment->size = $fileSize;
  49. $attachment->file_type = $fileMimeType;
  50. if ($attachment->save()) {
  51. return env('APP_URL').'/attachment/'.$md5;
  52. } else {
  53. @unlink($real_path);
  54. return false;
  55. }
  56. } catch (FileException $e) {
  57. return false;
  58. }
  59. }
  60. }