zoukankan      html  css  js  c++  java
  • laravel5.6上传图片及显示

    借鉴大神博客:https://blog.csdn.net/tony_110/article/details/80105099
    文档:http://laravelacademy.org/post/8965.html

    在config下新建文件admin.php,定义上传文件的路径

    'upload_img_path'                       =>'app/public/img',//本地上传图片路径
    'upload_file_path'                       =>'app/public/files'//本地上传文件路径

    在config/filesystems.php下定义

    'disks' => [
        'uploadimg'=>[
            'driver'=>'local',
            'root'=>storage_path(config('admin.upload_img_path'))
        ],
        'uploadfiles'=>[
            'driver'=>'local',
            'root'=>storage_path(config('admin.upload_file_path'))
        ],
    
        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],
    
        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],
    
        's3' => [
            'driver' => 's3',
            'key' => env('AWS_KEY'),
            'secret' => env('AWS_SECRET'),
            'region' => env('AWS_REGION'),
            'bucket' => env('AWS_BUCKET'),
        ],
    

     前端显示

    {{ html()->form('POST', route('frontend.repair.repair.upload'))
    ->attribute('enctype', 'multipart/form-data')->attribute('files', 'ture')->class('form-horizontal')->open() }}
    (红色标注的很重要)
    Controller中
    use IlluminateSupportFacadesStorage;

    public function sendmsg(SendmsgRequest $request)
    {
    $file = $request->file('cover');

    //保存图片begin
    $rule = ['jpg', 'png', 'gif'];
    if ($request->hasFile('cover')) {//验证是否上传图片
    $clientName = $file->getClientOriginalName();// 文件原名
    $tmpName = $file->getFileName();
    $realPath = $file->getRealPath();//临时文件的绝对路径
    $entension = $file->getClientOriginalExtension();// 扩展名
    if (!in_array($entension, $rule)) {
    return '图片格式为jpg,png,gif';
    }
    $newName = md5(date("Y-m-d H:i:s") . $clientName) . "." . $entension;//图片重命名
    $bool = Storage::disk('uploadimg')->put($newName, file_get_contents($realPath));//保存图片
    //return back();
    //return json_encode(['status' => 1, 'filepath' => $newName]);
    } else {
    $idCardFrontImg = '';
    //return json_encode($idCardFrontImg);
    $newName=1;
    }

    //保存图片end
    }
    根据文档中的要求composer安装了三项东西。

    显示时:
    <th><img src="/storage/img/pic_name"></th><!--根据数据库中报存的图片名称显示图片-->

    注:一定要执行
    php artisan storage:link
    执行后public/storage文件夹上会出现链接的标志,如果没有则删除storage文件夹,重新执行。
     
  • 相关阅读:
    MySQL--字符集参数
    MySQL--字符集基础
    Cassandra基础2
    Cassandra基础
    Cassandra -- Cassandra 3.0版本安装
    Cassandra Demo--Python操作cassandra
    MySQL--批量插入导致自增跳号问题
    MySQL Disk--SSD和HDD的性能
    MySQL Lock--并发插入导致的死锁
    TCL-视图
  • 原文地址:https://www.cnblogs.com/webttt/p/9323278.html
Copyright © 2011-2022 走看看