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