| Total Complexity | 21 |
| Total Lines | 116 |
| Duplicated Lines | 0 % |
| Coverage | 0% |
| Changes | 0 | ||
| 1 | <?php |
||
| 16 | class UploadedFile extends File |
||
|
1 ignored issue
–
show
|
|||
| 17 | { |
||
| 18 | |||
| 19 | private $test = false; |
||
| 20 | private $originalName; |
||
| 21 | private $mimeType; |
||
| 22 | private $error; |
||
| 23 | |||
| 24 | public function __construct(string $path, string $originalName, string $mimeType = null, int $error = null, $test = false) |
||
| 31 | } |
||
| 32 | |||
| 33 | public function isValid() |
||
| 34 | { |
||
| 35 | $isOk = UPLOAD_ERR_OK === $this->error; |
||
| 36 | |||
| 37 | return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname()); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function move(string $directory, $name = null) |
||
| 41 | { |
||
| 42 | if ($this->isValid()) { |
||
| 43 | if ($this->test) { |
||
| 44 | return parent::move($directory, $name); |
||
| 45 | } |
||
| 46 | |||
| 47 | $target = $this->getTargetFile($directory, $name); |
||
| 48 | |||
| 49 | set_error_handler(function ($type, $msg) use (&$error) { |
||
| 50 | $error = $msg; |
||
| 51 | }); |
||
| 52 | |||
| 53 | $moved = move_uploaded_file($this->getPathname(), $target); |
||
| 54 | restore_error_handler(); |
||
| 55 | if (!$moved) { |
||
| 56 | throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error))); |
||
| 57 | } |
||
| 58 | |||
| 59 | @chmod($target, 0666 & ~umask()); |
||
| 60 | |||
| 61 | return $target; |
||
| 62 | } |
||
| 63 | |||
| 64 | throw new FileException($this->getErrorMessage()); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * 获取错误信息 |
||
| 69 | * @access public |
||
| 70 | * @return string |
||
| 71 | */ |
||
| 72 | protected function getErrorMessage(): string |
||
| 73 | { |
||
| 74 | switch ($this->error) { |
||
| 75 | case 1: |
||
|
1 ignored issue
–
show
|
|||
| 76 | case 2: |
||
|
1 ignored issue
–
show
|
|||
| 77 | $message = 'upload File size exceeds the maximum value'; |
||
| 78 | break; |
||
| 79 | case 3: |
||
|
1 ignored issue
–
show
|
|||
| 80 | $message = 'only the portion of file is uploaded'; |
||
| 81 | break; |
||
| 82 | case 4: |
||
|
1 ignored issue
–
show
|
|||
| 83 | $message = 'no file to uploaded'; |
||
| 84 | break; |
||
| 85 | case 6: |
||
|
1 ignored issue
–
show
|
|||
| 86 | $message = 'upload temp dir not found'; |
||
| 87 | break; |
||
| 88 | case 7: |
||
|
1 ignored issue
–
show
|
|||
| 89 | $message = 'file write error'; |
||
| 90 | break; |
||
| 91 | default: |
||
|
1 ignored issue
–
show
|
|||
| 92 | $message = 'unknown upload error'; |
||
| 93 | } |
||
| 94 | |||
| 95 | return $message; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * 获取上传文件类型信息 |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function getOriginalMime() |
||
| 103 | { |
||
| 104 | return $this->mimeType; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * 上传文件名 |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | public function getOriginalName() |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * 获取上传文件扩展名 |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function getOriginalExtension() |
||
| 121 | { |
||
| 122 | return pathinfo($this->originalName, PATHINFO_EXTENSION); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * 获取文件扩展名 |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | public function extension() |
||
| 132 | } |
||
| 133 | } |
||
| 134 |