1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?php
- namespace App\Servers;
- // 文件上传
- use Illuminate\Http\Request;
- class FileServer
- {
- /**
- * @param Request $request
- * @param string $file
- * @param string $path
- * @param int $size
- * @return array
- *
- * 上传单张图片
- */
- static public function imageOne(Request $request, $file = 'file', $path = 'image', $size = 5)
- {
- if( $request->isMethod('post') ){
- $returnDatum = array('errcode'=>1,'errmsg'=>'','url'=>'');
- if( $request->hasFile($file) ){
- $ext = $request->file($file)->extension(); // 文件后缀判断
- if( !in_array($ext,["jpg", "jpeg", "gif", "png", "bmp", "webp"]) ){
- $returnDatum['errmsg'] = config('lang', 'zh') === 'zh' ? '上传图片仅支持png、jpg、jpeg、gif、bmp、webp后缀!!!' : 'stand by png、jpg、jpeg、gif、bmp、webp';
- return $returnDatum;
- }
- // 文件大小是否满足 5M = 5*1024*1024 B
- if( $request->file($file)->getClientSize() > $size * 1048576 ){
- $returnDatum['errmsg'] = config('lang', 'zh') === 'zh' ? '上传图片大小超过5M!!!' : 'Upload image size exceeds 5M.';
- return $returnDatum;
- }
- // 上传过程是否出错
- if( ! $request->file($file)->isValid() ){
- $returnDatum['errmsg'] = config('lang', 'zh') === 'zh' ? '上传文件出错,请重试!!!' : 'Error uploading file, please try again.';
- return $returnDatum;
- }
- // 文件保存 生成一个唯一的文件名称
- $fileName = time().str_random(4).mt_rand(1000,9999).".{$ext}";
- $pathUrl = $request->file($file)->storeAs($path.'/'.date('y-m',time()),$fileName,'public');
- if( $pathUrl ){
- $returnDatum['errcode'] = 0;
- $returnDatum['url'] = asset('storage').DIRECTORY_SEPARATOR.$pathUrl;
- return $returnDatum;
- }else{
- $returnDatum['errmsg'] = config('lang', 'zh') === 'zh' ? '图片上传保存失败,请重试!!!' : 'Image upload failed to save, please try again.';
- return $returnDatum;
- }
- }else{
- $returnDatum['errmsg'] = config('lang', 'zh') === 'zh' ? '请上传图片文件!!!' : 'Please upload an image file.';
- return $returnDatum;
- }
- }else{
- return array('errcode'=>1,'errmsg'=>'无效请求!','url'=>'');
- }
- }
- }
|