UploadedFile.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace think\file;
  13. use think\exception\FileException;
  14. use think\File;
  15. class UploadedFile extends File
  16. {
  17. private $test = false;
  18. private $originalName;
  19. private $mimeType;
  20. private $error;
  21. public function __construct(string $path, string $originalName, string $mimeType = null, int $error = null, bool $test = false)
  22. {
  23. $this->originalName = $originalName;
  24. $this->mimeType = $mimeType ?: 'application/octet-stream';
  25. $this->test = $test;
  26. $this->error = $error ?: UPLOAD_ERR_OK;
  27. parent::__construct($path, UPLOAD_ERR_OK === $this->error);
  28. }
  29. public function isValid(): bool
  30. {
  31. $isOk = UPLOAD_ERR_OK === $this->error;
  32. return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname());
  33. }
  34. /**
  35. * 上传文件
  36. * @access public
  37. * @param string $directory 保存路径
  38. * @param string|null $name 保存的文件名
  39. * @return File
  40. */
  41. public function move(string $directory, string $name = null): File
  42. {
  43. if ($this->isValid()) {
  44. if ($this->test) {
  45. return parent::move($directory, $name);
  46. }
  47. $target = $this->getTargetFile($directory, $name);
  48. set_error_handler(function ($type, $msg) use (&$error) {
  49. $error = $msg;
  50. });
  51. $moved = move_uploaded_file($this->getPathname(), $target);
  52. restore_error_handler();
  53. if (!$moved) {
  54. throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error)));
  55. }
  56. @chmod($target, 0666 & ~umask());
  57. return $target;
  58. }
  59. throw new FileException($this->getErrorMessage());
  60. }
  61. /**
  62. * 获取错误信息
  63. * @access public
  64. * @return string
  65. */
  66. protected function getErrorMessage(): string
  67. {
  68. switch ($this->error) {
  69. case 1:
  70. case 2:
  71. $message = 'upload File size exceeds the maximum value';
  72. break;
  73. case 3:
  74. $message = 'only the portion of file is uploaded';
  75. break;
  76. case 4:
  77. $message = 'no file to uploaded';
  78. break;
  79. case 6:
  80. $message = 'upload temp dir not found';
  81. break;
  82. case 7:
  83. $message = 'file write error';
  84. break;
  85. default:
  86. $message = 'unknown upload error';
  87. }
  88. return $message;
  89. }
  90. /**
  91. * 获取上传文件类型信息
  92. * @return string
  93. */
  94. public function getOriginalMime(): string
  95. {
  96. return $this->mimeType;
  97. }
  98. /**
  99. * 上传文件名
  100. * @return string
  101. */
  102. public function getOriginalName(): string
  103. {
  104. return $this->originalName;
  105. }
  106. /**
  107. * 获取上传文件扩展名
  108. * @return string
  109. */
  110. public function getOriginalExtension(): string
  111. {
  112. return pathinfo($this->originalName, PATHINFO_EXTENSION);
  113. }
  114. /**
  115. * 获取文件扩展名
  116. * @return string
  117. */
  118. public function extension(): string
  119. {
  120. return $this->getOriginalExtension();
  121. }
  122. }