文件上传
Laravel的文件系统是基于Frankde Jonge的Flysystem扩展包
提供了简单的接口,可以操作本地端空间,Amazons3,Rackspace Cloud Storage
可以非常简单的切换不同保存方式,但仍使用相同的API操作
配置文件
1.Config/filesystems.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' =>storage_path('app/public'),
'visibility' => 'public',
],
//新建立一个uploads
'uploads' =>[
'driver' => 'local',
//storage_path对应的是目录下storage
'root' =>storage_path('app/uploads'),
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => 'your-key',
'secret' => 'your-secret',
'region' => 'your-region',
'bucket' => 'your-bucket',
],
],
2.view
<form class="form-horizontal" method="POST" action="" enctype="multipart/form-data">
{{csrf_field() }}
<div class="form-group{{$errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">请选择文件</label>
<div class="col-md-6">
<input id="file" type="file" class="form-control" name="source" required>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
确认上传
</button>
</div>
</div>
</form>
3.controllers
public function upload(Request $request)
{
if($request->isMethod('POST')) {
//print_r($_FILES);
//source表单名
$file = $request->file('source');
//文件是否上传成功
if($file->isValid()) {
//取原文件名
$originalName = $file->getClientOriginalName();
//取扩展名
$ext = $file->getClientOriginalExtension();
//取文件类型
$type = $file->getClientMimeType();
//临时文件的绝对路径
$realPath = $file->getRealPath();
//定义文件名
$fileName = date("YmdHis"). '-' . uniqid(). '.' . $ext;
//uploads为config/filesystems里自定义
$bool = Storage::disk('uploads')->put($fileName, file_get_contents($realPath));
print_r($bool);exit;
}
}
return view('student.upload');
}