file = request()->file($name); if(!empty($file)) throw new UploadFileException(100012); $this->file_info = [ 'name' => $this->file->getOriginalName(),//文件原始名称 'mime' => $this->file->getOriginalMime(),//上传文件类型信息 'real_path' => $this->file->getRealPath(),//上传文件真实路径 'ext' => $this->file->getOriginalExtension(),//上传文件后缀 'size' => $this->file->getSize(),//上传文件大小 ]; if($is_rename){ $this->file_name = $this->createFileName(); }else{ $this->file_name = $this->file_info['name']; } } /** * 校验文件是否合法 */ public function check(){ } /** * 生成新的文件名 * @return string */ public function createFileName(string $key = '', string $ext = ''){ //DIRECTORY_SEPARATOR 常量 if(empty($key)){ return time().md5($this->file_info['real_path']).'.'.$this->file_info['ext']; }else{ return time().md5($key).'.'.$ext; } } /** * 获取原始附件信息 * @return mixed */ public function getFileInfo(){ return $this->file_info; } /** * 获取上传文件的真实完整路径 * @return mixed */ public function getRealPath(){ return $this->file_info['real_path']; } /** * 获取生成的文件完整地址 * @return mixed */ public function getFullPath(){ return $this->full_path; } /** * 合并路径和文件名 */ public function concatFullPath(string $dir = ''){ $this->full_path = implode('/', array_filter([$dir, $this->getFileName()])); } /** * 获取文件名 * @return mixed */ public function getFileName() { return $this->file_name; } public function getUrl(string $path = ''){ $path = !empty($path) ? $path : $this->getFullPath(); $domain = $this->config['domain'] ?? ''; $domain = empty($domain) ? '' : $domain.'/'; return $domain.$path; } }