我是Laravel開發中的新手。我幾乎沒有文字記錄以及1個視頻上傳和縮略圖上傳功能。我可以將所有這些數據保存到數據庫中,但我堅持使用視頻和縮略圖/圖片上傳。在Laravel 5.1上傳視頻和縮略圖
這裏是我的控制器代碼:
public function save(Request $request)
{
// Any other fields to be saved here..
$post = $request->all();
// var_dump($post);
$v = \Validator::make($request->all(),
[
'title' => 'required',
'category' => 'required',
'description' => 'required',
'price' => 'required|Numeric',
'discount' => 'Numeric',
'thumbnail' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]
);
$file = Input::file('thumbnail');
$destinationPath = 'images/';
$filename = $file->getClientOriginalName();
Input::file('thumbnail')->move($destinationPath, $filename);
if($v->fails())
{
return redirect()->back()->withErrors($v->errors());
}
else
{
$data = array(
'title' => $post['title'],
'category' => $post['category'],
'partner' => $post['partner'],
'description' => $post['description'],
'published' => $post['published'],
'featured' => $post['featured'],
'price' => $post['price'],
'discount' => $post['discount'],
'file' => "file",
'thumbnail' => $filename
);
$i=DB::table('items')->insert($data);
if($i>0)
{
\Session::flash('message','new Item Saved');
return redirect('itemindex');
}
}
}
我添加了一些代碼來測試上傳圖片作爲縮略圖。但失敗了。
以下是你需要上傳文件(視頻/海報),然後保存上傳的路徑到你的數據庫的視圖
<div class="form-group">
<label for="Thumbnail" class="col-md-3 control-label"></label>
<div class="timeline-item">
<div class="col-md-9 ">
<div class="timeline-body">
<img src="http://placehold.it/150x100" alt="..." class="margin">
</div>
</div>
</div>
</div>
什麼問題了嗎? –
有人會認爲$ data數組中的註釋行是問題,請更新問題以便更清楚地問你在問什麼。 –
@RisulIslam 我寫了這段代碼,但不能用於文件上傳。 $ file = Input :: file('thumbnail'); $ destinationPath ='images /'; $ filename = $ file-> getClientOriginalName();輸入:: file('thumbnail') - > move($ destinationPath,$ filename); –