introduction
I have been unemployed recently and I go to the library to read a lot of books every day.
Famous quotes:
There are many opportunities in a person’s life, only one important opportunity
Entrepreneurship success is definitely not based on technology, but on the market
The story of Giant Company, Corporate Helping Poverty, Apple VS Noah, How Microsoft Rises
How to relieve stress and fight anxiety
Planar composition (symmetry and balance, contrast and harmony)
Emotional intelligence class
1. Be concerned about the other person (just as warmth as parents talk to their children, is the weather cold?)
2. Learn to apologize, did you scare you (sorry for causing trouble for you)
3. Resonance (Then let’s turn on the air conditioner)
The following code
<?php namespace App\Http\Controllers\Api\Air\v_1_0; use App\Http\Controllers\Core\ApiController; use App\Models\LoginLog; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use OSS\OssClient; class FileController extends ApiController { public function uploadFile(Request $request) { $file = $request->file(); $ret = $this->_upload($file); echo json_encode($ret); exit; } protected function _upload($file) { $dir_name = empty($_GET['dir']) ? 'image' : trim($_GET['dir']); $storage_path = config(''); $webpath_path = config(''); $image_path = $dir_name . '/' . date("Y-m") . '/'; $app_url = env('APP_URL') . '/'; if (!$file) { return array('error'=>1,'message'=>"File upload failed, please check and try again"); } $ossConfig = config('oss.' . config('')); if($ossConfig['bucket_addr']) { $app_url = $ossConfig['bucket_addr']; } foreach ($file as $k=>$v) { $tmpName = $v->getPathName(); $fileExtension = $v->getClientOriginalExtension(); $filePath = md5_file($tmpName) . '.' . $fileExtension; $file_size = $v->getSize(); if($file_size > config('')) { return array('error'=>1,'message'=>"The file cannot exceed 50MB"); } if (!in_array(strtolower($fileExtension),config(''))) { return array('error'=>1,'message'=>"File type not supported"); } //After uploading is successful, processing if(config('oss.' . 'oss_open') == 1) { $oss = new OssClient($ossConfig['access_key_id'], $ossConfig['access_key_secret'], $ossConfig['endpoint']); $res = $oss->uploadFile($ossConfig['bucket'],$webpath_path . $image_path . $filePath,$v->getPathName()); if ($res) { $data['oss'] = 1; $data['attach_url'] = $ossConfig['bucket_addr'] . $webpath_path . $image_path . $filePath; } else { $data['attach_url'] = $app_url . $webpath_path . $image_path . $filePath; $data['oss'] = 0; } $data['time'] = time(); $attach_id = DB::table('attachment')->insertGetId($data); $attach_array[] = $attach_id; $attach_url[] = $data['attach_url']; } else { $data['oss'] = 0; $data['attach_url'] = $app_url. $webpath_path . $image_path . $filePath; $data['time'] = time(); $attach_id = DB::table('attachment')->insertGetId($data); $attach_array[] = $attach_id; $attach_url[] = $data['attach_url']; } if(!$data['oss'] || config('oss.' . 'is_delete') != 1) { $v->move($storage_path . $image_path, $filePath); unset($data); } return array('error'=>0,'attach'=>$attach_array,'url'=>$attach_url[0],'url_arr'=>$attach_url); } } }
The above is the detailed content of how Laravel uploads files. For more information about Laravel uploads, please pay attention to my other related articles!