审查视图

lib/UploadFile.php 3.8 KB
1  
邓超 authored
1 2 3 4
<?php

namespace Lib;
1  
邓超 authored
5
1  
邓超 authored
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
/**
 * 文件
 * @author:dc
 * @time 2023/3/13 14:34
 * Class UploadFile
 * @package Lib
 */
class UploadFile
{
    /**
     * @var string
     */
    public string $name;

    /**
     * @var string
     */
    public string $tempFile;

    /**
     * @var int
     */
    public int $size;

    /**
     * @var string
     */
    public string $mime;

    /**
     * @var string
     */
    public string $ext;

    /**
1  
邓超 authored
41 42 43 44 45 46 47
     * @var string
     */
    public string $saveName = '';

    public string $savePath = '';

    /**
1  
邓超 authored
48 49 50 51 52
     * @var bool
     */
    private $isUpload = true;

    /**
1  
邓超 authored
53 54 55 56 57 58 59 60 61
     * UploadFile constructor.
     * @param $name
     * @param $tempFile
     */
    public function __construct(string $name, string $tempFile)
    {

        $this->name = $name;
1  
邓超 authored
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
        // 不是临时文件
        if(!is_file($tempFile)){
            $this->isUpload = false;
            // 是不是远程路径
            if(str_starts_with($tempFile, 'http://') || str_starts_with($tempFile, 'https://')){
                $content = @file_get_contents($tempFile);
            }
            // 以文件内容的形式保存
            else{
                $content = $tempFile;
            }

            if(!$content){
                throw new Err('upload_file_load_error',600);
            }

            // 临时路径
            $tempFile = PUBLIC_PATH.'/temp/'.md5($tempFile);

            if(!is_dir(dirname($tempFile))){
                @mkdir(dirname($tempFile),0775,true);
            }

            // 保存失败
            if(!@file_put_contents($tempFile,$content)){
                throw new Err('upload_file_load_error',601);
            }

        }

1  
邓超 authored
93 94 95 96 97 98 99 100 101 102
        $this->tempFile = $tempFile;

        // kb
        $this->size = (int) (filesize($this->tempFile)/1024);
        // 文件类型
        $this->mime = mime_content_type($this->tempFile);

        $ext = explode('.',$name);
        $this->ext = end($ext);
x  
邓超 authored
103
        $this->savePath = PUBLIC_PATH;
1  
邓超 authored
104
1  
邓超 authored
105 106 107
    }

1  
邓超 authored
108 109 110 111 112 113 114 115 116 117 118 119 120
    /**
     * @param null $name
     * @return bool
     * @author:dc
     * @time 2023/4/6 13:58
     */
    public function move($name = null){

        if(!is_dir($this->savePath)){
            @mkdir($this->savePath,0775,true);
        }

        // 保存的文件
x  
邓超 authored
121
        $this->saveName =   '/storage/'.($name ? $name : md5_file($this->tempFile).'.'.$this->ext);
1  
邓超 authored
122
x  
邓超 authored
123 124 125 126 127
        // 验证目录
        if(!is_dir(dirname($this->savePath.$this->saveName))){
            @mkdir(dirname($this->savePath.$this->saveName),0775,true);
        }
1  
邓超 authored
128 129 130 131 132 133 134
        if($this->isUpload){
            return move_uploaded_file($this->tempFile,$this->savePath.$this->saveName);
        }else{
            $ret = copy($this->tempFile,$this->savePath.$this->saveName);
            @unlink($this->tempFile);
            return $ret;
        }
1  
邓超 authored
135 136 137

    }
1  
邓超 authored
138 139

x  
邓超 authored
140 141 142 143 144 145 146 147 148 149 150 151 152
    /**
     * 验证
     * @param array $rule
     * @param bool $throw
     * @throws Err
     * @author:dc
     * @time 2023/4/18 10:55
     */
    public function verify(array $rule,bool $throw = true){
        // 后缀
        if(!empty($rule['ext'])){
            if (!in_array($this->ext,$rule['ext'])){
                if(!$throw) return false;
x  
邓超 authored
153
                app()->e(['verify_file.ext',[' ',$this->name,implode('|',$rule['ext'])]],600);
x  
邓超 authored
154 155 156 157 158 159 160
            }
        }

        // 类型
        if(!empty($rule['mine'])){
            if (!in_array($this->mime,$rule['mine'])){
                if(!$throw) return false;
x  
邓超 authored
161
                app()->e(['verify_file.mine',[' ',$this->name,implode('|',$rule['mine'])]],600);
x  
邓超 authored
162 163 164 165 166 167 168
            }
        }

        // 大小
        if(!empty($rule['size'])){
            if ($this->size > $rule['size']){
                if(!$throw) return false;
x  
邓超 authored
169
                app()->e(['verify_file.size',[' ',$this->name,$rule['size']]],600);
x  
邓超 authored
170 171
            }
        }
x  
邓超 authored
172
        return true;
x  
邓超 authored
173 174 175
    }

1  
邓超 authored
176
}