阿里云OSS上传

2021-09-29  本文已影响0人  Sen_森

composer 安装 JohnLui\AliyunOSS

创建OSS文件(百度云提取码)

链接:https://pan.baidu.com/s/1bhWg1C-bchsah-iDEbr7pA 
提取码:b9ma 

使用(控制器)

<?php
/**
 * Created by PhpStorm.
 * User: Moker-T
 * Date: 2019/4/28
 * Time: 16:32
 */

namespace App\Http\Controllers\Home;

use App\Services\OSS;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Urls;


class FileController extends Controller
{
    //文件上传表单
    public function upload()
    {
        return view('home.upload');
    }

    public function doUpload(Request $request)
    {
        $files = $request->file('multipleFileUpload');

        $content = $_POST['content'];

        if (!$files) {
            return redirect('/upload')->with('message', '请选择文件!!!');
        }
        if(!$content){
            return redirect('/upload')->with('message', '请输入描述!!!');
        }
        //上传至阿里云OSS
        foreach ($files as $k => $v) {
            $tmppath[] = $v->getRealPath();

            $fileName[] = str_random(5) . time() . date('ymd') . '.' . $v->getClientOriginalExtension();

            $pathName[] = date('Y-m/d') . '/' . $fileName[$k];

            OSS::publicUpload('moker-pdf', $pathName[$k], $tmppath[$k], ['ContentType' => $v->getClientMimeType()]);

            $Url[$k] = OSS::getPublicObjectURL('moker-pdf', $pathName[$k]);
        }
        $time =  date("Y-m-d H:i:s");
        foreach ($Url as $a => $b) {
            $u[$b] = 'url';
            $u[$content] = 'content';
            $u[ $time] = 'createtime';
            $Urls[$a] = array_flip($u);

        }
//        print_r( $Urls);die;
        $res = Urls::insetUrls($Urls);
        if (!$res) {
            return redirect('/upload')->with('message', '内部系统错误');
        }
        return redirect('/upload')->with('message', '上传成功!!!');


    }

}
上一篇下一篇

猜你喜欢

热点阅读