分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

laravel5.6上传图片及显示

发布时间:2023-09-06 02:04责任编辑:顾先生关键词:上传图片
借鉴大神博客:https://blog.csdn.net/tony_110/article/details/80105099
文档:laravel5.6文档 文件传输

在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 Illuminate\Support\Facades\Storage;

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><!--根据数据库中报存的图片名称显示图片-->

laravel5.6上传图片及显示

原文地址:https://www.cnblogs.com/webttt/p/9323278.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved