>在 Web 应用程序中,最常见的例子之一就是存储文件是存储用户上传的文件,如个人资料图片,照片和文档。 Laravel 通过使用 store 方法,使得它在上传文件的实例中非常容易的存储上传了的文件。 根据 Laravel 的 store 方法,实现文件上传功能,并扩展方法,使其支持云存储。 ### 创建 upload.php ```php filesystem = env('STORAGE_DISK', 'local'); //local,qiniu,oss $this->disk = Storage::disk($this->filesystem); } /** * 文件上传 * @param string $file * @param string $type * @return array */ public function store($file, $type) { $this->error = null; $this->ret = null; $this->type = $type; $this->file = $file; $this->file_name = $this->get_name(); $this->save_path = $this->type . '/' . $this->file_name; return $this->upload(); } /** * 执行上传 * @return array */ public function upload() { $this->error = $this->check($this->save_path); if($this->error) { return array($this->ret, $this->error); } return array(array( 'fileName' => $this->file_name, 'fileType' => $this->file->getClientMimeType(), 'fileSize' => $this->file->getClientSize(), 'filePath' => $this->file_path(), 'webPath' => $this->file_web(), 'type' => $this->type, ), $this->error); } /** * 校验并上传文件 * @return string|NULL */ public function check() { if(!$this->file->isValid()) { return '附件错误:文件上传出错'; } if(!$this->att_type()) { return '附件错误:文件类型不正确'; } if(!$this->att_size()) { return '附件错误:文件大小超出'; } if($this->disk->exists($this->save_path)) { return '附件错误:文件已存在'; } if(!$this->disk->put($this->save_path, file_get_contents($this->file->getPathname()))) { return '上传错误:文件上传失败'; } if(!$this->disk->exists($this->save_path)) { return '上传错误:保存文件失败!'; } return null; } /** * 根据文件名删除文件 * @param string|array $file * @param string $type * @return boolean */ public function destroy($file, $type) { $this->type = $type; if(is_string($file)) { return $this->disk->delete($this->type . '/' . $file); } $file = array_map(function($value) { return $this->type . '/' . $value; }, $file); return $this->disk->delete($file); } /** * 删除文件夹及里面的文件 * @param string $type */ public function deleteDirectory($type) { $this->type = $type; $this->disk->deleteDirectory($this->type); } /** * 根据类型获取文件夹下的所有文件 * @param string $type * @return array */ public function get_file($type) { $aFile = $this->disk->files($type.'/'); return array_map(function($path) { $aPath = explode('/', $path); return $aPath[1]; }, $aFile); } /** * 根据类型获取文件夹下的所有文件 -- 全信息 * @param string $type */ public function get_file_full($type) { $this->type = $type; $aFile = $this->disk->files($type.'/'); $array = array(); return array_map(function($file) use ($array) { $aPath = explode('/', $file); $array['file'] = $aPath[1]; $array['type'] = $this->disk->mimeType($file); $array['size'] = $this->human_filesize($this->disk->size($file)); $array['time'] = $this->disk->lastModified($file); $array['path'] = $this->file_web().'/'.$aPath[1]; return $array; }, $aFile); } /** * 根据上传类型判断文件储存位置 */ public function file_path() { if($this->filesystem == 'local') { return rtrim(config('filesystems.disks.local.root'), '/') . '/' . $this->type; } else { return '/' . $this->type.'/'; } } /** * 返回文件完整的web路径 */ public function file_web($type = '') { if(!empty($type)) { $this->type = $type; } if($this->filesystem == 'local') { return rtrim(config('filesystems.disks.local.root'), '/') . '/' . $this->type; } else if ($this->filesystem == 'qiniu') { return rtrim(config('filesystems.disks.qiniu.domains.custom'), '/') . '/' . $this->type; } else { return rtrim(config('filesystems.disks.oss.custom'), '/') . '/' . $this->type; } } /** * 随机文件名 * @return string */ public function get_name() { return md5(time() . rand(0,10000)) . '.' . $this->file->getClientOriginalExtension(); } /** * 返回可读性更好的文件尺寸 */ public function human_filesize($bytes, $decimals = 2) { $size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB']; $factor = floor((strlen($bytes) - 1) / 3); return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) .@$size[$factor]; } /** * 判断大小 * @return boolean */ public function att_size() { if($this->type == 'attachment') { $this->maxsize = 1024*1024*option('article_attsize'); } else { $this->maxsize = 1024*1024*option('img_attsize'); } if($this->file->getClientSize() > $this->maxsize) { return false; } return true; } /** * 上传的附件类型 */ public function att_type() { $mimeType = $this->file->getClientMimeType(); if($this->type !== 'attachment') { return starts_with($mimeType, 'image/'); } else { $atttype = explode(',', option('article_atttype')); foreach ($atttype as $k=>$value) { if(str_contains($mimeType, $value)) { return true; } } return false; } } } ?> ```