[英]How to upload image in laravel 5.2
這是我的表格
@extends('layout.template')
@section('content')
<h1>Add Student</h1>
{!! Form::open(array('action' => 'studentController@save', 'files'=>true)) !!}
<div class="form-group">
{!! Form::label('Profile-Picture', 'Profile Picture:') !!}
{!! Form::file('image',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Save', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
@stop
這是我的控制器方法
public function save()
{
$students=Request::all();
students::create($students);
Session::flash('flash_message', 'Record successfully added!');
return redirect('students');
}
當我上傳圖像並提交圖像時,比在數據庫列字段中保存該圖像地址“ / tmp / phpFFuMwC”;
那是因為您要保存臨時生成的文件URL。
對於文件,您需要手動將其保存到所需位置(確保其通過驗證):
$request->file('photo')->move($destinationPath);
// With a custom filename
$request->file('photo')->move($destinationPath, $fileName);
然后將新文件名(帶有或不帶有path)存儲在數據庫中,如下所示:
$students = new Students;
$students->image = $fileName;
...
$students->save();
在您的控制器上進行這些更改
public function save(Request $request)
{
$destination = 'uploads/photos/'; // your upload folder
$image = $request->file('image');
$filename = $image->getClientOriginalName(); // get the filename
$image->move($destination, $filename); // move file to destination
// create a record
Student::create([
'image' => $destination . $filename
]);
return back()->withSuccess('Success.');
}
別忘了使用
use Illuminate\Http\Request;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.