目录
一、上传文件的方法
1、上传到指定驱动和根目录
驱动:local,s3 等
方法:put、putFile、store 等
默认驱动为 local,根目录为 storage/app,在 config/filesystem.php 中指定,文件存储不能跳出根目录。一般在根目录下指定一个类似名为 uploads 的目录作为保存文件的目录。
// local 驱动 根目录为 storage/app,在 config/filesystems.php 中设置
$path = Storage::put('upload_put', $request->file('avatar')); //指定路径
$path = Storage::disk('local')->put('newpath', $request->file('avatar')); //指定磁盘,指定路径
$path = Storage::putFile('newpath', $request->file('avatar')); //指定路径
$path = Storage::putFileAs('newpath', $request->file('avatar'), 'newname'); //指定路径,文件名
$path = $request->file('avatar')->store('newpath'); //指定路径
$path = $request->file('avatar')->store('newpath', 'driver'); //指定路径,磁盘(s3,local)
$path = $request->file('avatar')->storeAs('newpath', 'newname'); //指定路径,文件名
2、上传到站点目录,通过移动文件方式上传
方法:move
默认目录为 public,文件存储可以跳出 public 目录。一般在 public 目录下指定一个类似名为 uploads 的目录作为保存文件的目录,如果想指定到其他目录,如 resource 目录,可以使用 move 方法。
// 根目录为 public
$path = $request->file('avatar')->move('newpath'); //指定路径
$path = $request->file('avatar')->move('newpath', 'newfilename'); //指定路径,文件名
二、上传文件的例子
1、注册路由
Route::any('admin/index/dbop3', 'Admin\IndexController@dbop3');
2、添加模型类
(1)产生模型
php artisan make:model Models/Stand
(2)模型属性
模型对应到表,注意添加 可写白名单($fillable 属性)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Stand extends Model
{
//
protected $table = "stand"; //必须
protected $primaryKey = "id"; //必须
public $timestamps = false;
protected $fillable = ['id', 'name', 'age', 'avatar']; //允许写入字段,才能够写入数据
}
3、控制器方法
<?php
namespace App\Http\Controllers\Admin;
use bar\baz\source_with_namespace;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use DB;
use App\Models\Post;
use App\Models\Stand;
class IndexController extends Controller
{
public function dbop3(Request $request){
if(Input::method() == 'POST'){
// 选择了文件(文件存在) & 文件上传成功(文件有效)
if($request->hasFile('avatar') && $request ->file('avatar')->isValid()){
//返回文件信息
dump($request->file('avatar')->getClientOriginalName());
dump($request->file('avatar')->getClientOriginalExtension());
echo ($request->file('avatar')->getClientSize()) . ' B';
//保存文件,指定路径,设定文件名
$filepath = './uploads';
$filename = md5(time() . rand(100000, 999999)) . '.' . $request->file('avatar')->getClientOriginalExtension();
$request->file('avatar')->move($filepath, $filename);
//将数据保存到数据库
$data = $request->all();
$data['avatar'] = $filepath . '/' . $filename; // 注意添加可写属性 fillable
dump($data);
$res = Stand::create($data);
echo $res;
}
}else{
return view('Admin.dbop3');
}
}
}
4、表单视图
(1)视图代码
视图文件为 Admin/dbop3.blade.php
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<title>表单</title>
</head>
<body>
<form action="/admin/index/modelinsert3", method="post">
<p>姓名:<input type="text" name="name" value=""></p>
<p>年龄:<input type="text" name="age" value=""></p>
<p>邮箱:<input type="email" name="email" value=""></p>
{{csrf_field()}}
<input type="submit" value="提交">
</form>
</body>
</html>
(2)浏览器访问
(3)提交测试
上传操作
网站返回
数据库展示