| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 | <?phpdeclare(strict_types=1);namespace app\common\model;use think\facade\Config;use think\File;use think\exception\FileException;class FileManager extends \think\Model{    protected $pk = "fileid";    protected $schema = [        'fileid'        => "int",      // '文件id'        'filename'      => "varchar",  // '文件名称'        'filesize'      => "int",      // '文件大小字节'        'filetime'      => "int",      // '文件上传时间'        'filepath'      => "varchar",  // '文件路径'        'fileextension' => "varchar",  // '文件扩展名'        'title'         => "varchar",  // '文件title'        'type'          => "int",      // '0为附件,1为图片,2为Flash文件,3为多媒体文件'        'onclick'       => "int",      // '下载量'        'username'      => "varchar",  // '上传者'        'infoid'        => "int",      // '信息ID',         'cjid'          => "int",       // '信息临时ID'        'hash_md5'       => "varcahr"       // hash值(MD5)    ];    protected $autoWriteTimestamp = false;    public static function queryPage(array $param = [])    {        $limit = (int)$param['limit'];        $where = [];        return self::where($where)->field('fileid,filename,filesize,filetime,filepath,fileextension,title,username')->order('fileid DESC')->paginate(['list_rows'=>$limit, 'query' => $param]);    }    public static function saveFile(File $file)    {        $fileinfo = self::where('hash_md5', $file->md5())->find();        $publicRootPath = str_replace('\\', '/', Config::get('filesystem.disks.public.root'));        $publicUrlPath =  Config::get('filesystem.disks.public.url');        if ($fileinfo == null) {            $fileinfo = new static();        } elseif ($publicRootPath . $fileinfo->filepath == $file->getPathname())  { // 路径不同的文件            $fileinfo = new static();        }        $fileinfo->filename      = $file->getFilename();        $fileinfo->filesize      = $file->getSize();        $fileinfo->filetime      = $file->getCTime();        $fileinfo->filepath      = $publicUrlPath . str_replace($publicRootPath, '', $file->getPathname());        $fileinfo->fileextension = $file->getExtension();        $fileinfo->hash_md5      = $file->md5();        $fileinfo->username      = 'system';        $fileinfo->save();    }    public static function saveFileInfo($file, $savename, $originalName = '', $id = 0, $cjid = 0, $username = 'system')    {        if (is_string($file)) {            $file = new File($file);        }        if (!$file->isFile()) {            throw new FileException('file not exist');        }        $publicUrlPath =  Config::get('filesystem.disks.public.url');        $fileinfo = new static();                        $fileinfo->filename      = basename($savename);        $fileinfo->filepath      = $publicUrlPath . '/'. $savename;        $fileinfo->title         = $originalName;        $fileinfo->id            = $id;        $fileinfo->cjid          = $cjid;        $fileinfo->username      = $username;        $fileinfo->filesize      = $file->getSize();        $fileinfo->filetime      = $file->getCTime();        $fileinfo->fileextension = $file->extension();        $fileinfo->hash_md5      = $file->md5();                $fileinfo->save();        return $fileinfo;    }}
 |