PHP 文件上传常规格式
PHP 文件上传常规格式
*相关扩展一定要打开
客户端代码:
<form action="{{ route('admin.article.store') }}"enctype="multipart/form-data" method="post" class="form form-horizontal" id="form-member-add"> <input type="file" name="pic"> </form>
服务器端代码:
AddArtRequest为自定义验证
public function store(AddArtRequest $request) { //文件上传 //默认封面图片 $pic = config('upload.pic'); if($request->hasFile('pic')) { //上传 //参数2 配置的节点 名称 article需要在filesystems.php 文件中自定义 $result = $request->file('pic')->store('','article'); // dump($result); $pic = '/uploads/article/'.$result; // dd($pic); } $post = $request->except('_token'); $post['pic'] = $pic; //添加 入库操作 Article::create($post); return redirect(route('admin.article.index')); }
filesysts.php
'disks' => [ 'local' => [ 'driver' => 'local', 'root' => storage_path('app'), ], //文章上传的封面 'article'=>[ 'driver'=>'local', 'root'=>public_path('uploads/article'), ], 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage', 'visibility' => 'public', ], 's3' => [ 'driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION'), 'bucket' => env('AWS_BUCKET'), 'url' => env('AWS_URL'), ], ],