huangsijun
2025-09-22 a78c011de350b188afb03beb2f26a73f35f71986
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
<?php
 
namespace app\common\library\storage\engine;
 
use think\facade\Request;
use think\Exception;
 
/**
 * 存储引擎抽象类
 */
abstract class Server
{
    protected $file;
    public $error;
    protected $fileName;
    protected $fileInfo;
 
    // 是否为内部上传
    protected $isInternal = false;
 
    /**
     * 构造函数
     */
    protected function __construct()
    {
    }
 
    /**
     * 设置上传的文件信息
     */
    public function setUploadFile($name)
    {
        // 接收上传的文件
        $this->file = Request::file($name);
        if (empty($this->file)) {
            throw new Exception('未找到上传文件的信息');
        }
        // 生成保存文件名
        $this->fileName = $this->buildSaveName();
    }
 
    /**
     * 设置上传的文件信息
     */
    public function setUploadFileByReal($filePath)
    {
        // 设置为系统内部上传
        $this->isInternal = true;
        // 文件信息
        $this->fileInfo = [
            'name' => basename($filePath),
            'size' => filesize($filePath),
            'tmp_name' => $filePath,
            'error' => 0,
        ];
        // 生成保存文件名
        $this->fileName = $this->buildSaveName();
    }
 
    /**
     * 文件上传
     */
    abstract protected function upload();
 
    /**
     * 文件删除
     */
    abstract protected function delete($fileName);
 
    /**
     * 返回上传后文件路径
     */
    abstract public function getFileName();
 
    /**
     * 返回文件信息
     */
    public function getFileInfo()
    {
        return $this->fileInfo;
    }
 
    protected function getRealPath()
    {
        $fileInfo = request()->file('iFile');
        return $fileInfo->getRealPath();
    }
 
    /**
     * 返回错误信息
     */
    public function getError()
    {
        return $this->error;
    }
 
    /**
     * 生成保存文件名
     */
    private function buildSaveName()
    {
        // 要上传图片的本地路径
        $realPath = $this->file->getPathname();
        // 扩展名
        $ext = $this->file->getOriginalExtension();
 
        // 自动生成文件名
        return date('YmdHis') . substr(md5($realPath), 0, 5)
            . str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT) . ".{$ext}";
    }
 
}