为了方便,先修改一个配置文件,再laravel框架中config配置中找到 filesystems.php 文件
修改代码如下
'local' => [ 'driver' => 'local', 'root' => public_path('image'), ], 'public' => [ 'driver' => 'local', 'root' => public_path('image'), 'url' => env('APP_URL').'image', 'visibility' => 'public', ],
以上配置将会在默认入口public目录中创建一个名为image的文件夹,你上传的图片将会在这个目录中
单文件上传
表单
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form action="up_do" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="上传"> </form> </body> </html>
提交方式为POST,提交到up_do方法
//图片上传 public function up_do(Request $request) { $data = $request->file('file'); $res = $data->store('');
//打印看一下上传成功文件的名字
echo $res; }
多文件上传
表单
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form action="up_do" method="post" enctype="multipart/form-data"> <input type="file" name="file[]"> <input type="file" name="file[]"> <input type="file" name="file[]"> <input type="submit" value="上传"> </form> </body> </html>
同理,文件名从单文件的name=“file”改成了数组的形式,表示上传多个文件
//图片上传 public function up_do(Request $request) { $data = $request->file('file'); //dd($data);die(); foreach($data as $k => $v) { $arr[$k] = $v->store('file'); } echo $arr; }
利用循环遍历上传到public中,新建文件夹名为file。实则路径:public/image/file
用时候png格式的图片上传失败,请换一种图片格式!!!