12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- <?php
- namespace App\Service;
- use Illuminate\Support\Facades\Storage;
- class FileUploadService extends Service
- {
- //文件类型
- const FILE_TYPE = [
- 'txt',
- 'jpg',
- 'png',
- 'gif',
- 'jpeg',
- 'zip',
- 'rar'
- ];
- public function uploadFile($file){
- // 获取文件相关信息
- $ext = $file->getClientOriginalExtension(); // 扩展名
- $realPath = $file->getRealPath(); //临时文件的绝对路径
- $ext = strtolower($ext);
- if (! in_array($ext, self::FILE_TYPE)){
- $str = '文件格式为:';
- foreach (self::FILE_TYPE as $value){
- $str.= $value . ' ' ;
- }
- return [false,$str];
- }
- // 上传文件
- $file_name = date("Ymd").time().rand(1000,9999);
- $filename = $file_name.'.' . $ext;
- // 使用我们新建的uploads本地存储空间(目录)
- Storage::disk('public')->put('upload_files/'.$filename, file_get_contents($realPath));
- return [true,'/api/uploadFiles/'.$file_name];
- }
- }
|