插件窝 干货文章 Laravel如何上传文件实现示例

Laravel如何上传文件实现示例

attach data path url 87    来源:    2024-10-29

引言

最近失业在家,每天去图书馆阅读大量的书籍。

名句:

人这一生机会很多,重要的机会只有一个
创业成功绝对不是靠技术,而是靠市场
巨人公司的故事、企业助穷、Apple VS 诺亚机、微软如何崛起
如何减压 And 对抗焦虑
平面构成(对称与平衡,对比与调和)

情商课

1. 关切对方(就像父母对话孩子一样的温暖,天气冷不冷啊)
2. 学会道歉,有没有吓到你了 (对不起给你添麻烦了)
3. 共鸣 (那我们把空调开起来吧)

如下代码

<?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('upload.storage');
        $webpath_path = config('upload.webpath');
        $image_path =  $dir_name . '/' . date("Y-m") . '/';
        $app_url = env('APP_URL') . '/';
        if (!$file) {
            return array('error'=>1,'message'=>"文件上传失败,请检查后重试");
        }
        $ossConfig = config('oss.' . config('upload.ossflag'));
        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('upload.maxSize'))
            {
                return array('error'=>1,'message'=>"文件不可以超过50MB");
            }
            if (!in_array(strtolower($fileExtension),config('upload.allowExts')))
            {
                return array('error'=>1,'message'=>"文件类型不支持");
            }
            //上传成功后处理
            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);
        }
    }
}

以上就是Laravel如何上传文件实现示例的详细内容,更多关于Laravel上传文件的资料请关注插件窝其它相关文章!