UploadFile.php 3.8 KB
<?php

namespace Lib;


/**
 * 文件
 * @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;

    /**
     * @var string
     */
    public string $saveName = '';

    public string $savePath = '';

    /**
     * @var bool
     */
    private $isUpload = true;

    /**
     * UploadFile constructor.
     * @param $name
     * @param $tempFile
     */
    public function __construct(string $name, string $tempFile)
    {

        $this->name = $name;

        // 不是临时文件
        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);
            }

        }


        $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);

        $this->savePath = PUBLIC_PATH.'/storage/';

    }


    /**
     * @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);
        }

        // 保存的文件
        $this->saveName =   $name ? $name : md5_file($this->tempFile).'.'.$this->ext;

        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;
        }

    }



    /**
     * 验证
     * @param array $rule
     * @param bool $throw
     * @throws Err
     * @author:dc
     * @time 2023/4/18 10:55
     */
    public function verify(array $rule,bool $throw = true){
        $this->lastVerifyError = '';
        // 后缀
        if(!empty($rule['ext'])){
            $rule['ext'] = is_string($rule['ext']) ? [$rule['ext']] : [];
            if (!in_array($this->ext,$rule['ext'])){
                if(!$throw) return false;
                app()->e(['verify_file.ext',['',$this->name,implode('|',$rule['ext'])]],600);
            }
        }

        // 类型
        if(!empty($rule['mine'])){
            $rule['mine'] = is_string($rule['mine']) ? [$rule['mine']] : [];
            if (!in_array($this->mime,$rule['mine'])){
                if(!$throw) return false;
                app()->e(['verify_file.mine',['',$this->name,implode('|',$rule['mine'])]],600);
            }
        }

        // 大小
        if(!empty($rule['size'])){
            if ($this->size > $rule['size']){
                if(!$throw) return false;
                app()->e(['verify_file.size',['',$this->name,$rule['size']]],600);
            }
        }
    }


}