2016-10-05 138 views
0

我是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> 
+0

什麼問題了嗎? –

+0

有人會認爲$ data數組中的註釋行是問題,請更新問題以便更清楚地問你在問什麼。 –

+0

@RisulIslam 我寫了這段代碼,但不能用於文件上傳。 $ file = Input :: file('thumbnail'); $ destinationPath ='images /'; $ filename = $ file-> getClientOriginalName();輸入:: file('thumbnail') - > move($ destinationPath,$ filename); –

回答

1

第一。

Laravels official documentation on file upload

$uniqueName = (integer)microtime(); // For unique naming vaideo/poster 
    $videoSrc = ""; 
    $thumbnailSrc = ""; 

    $file = $request->file('file');   
    // Upload video 
    $destinationPath = 'uploads/videos'; 
    $fileName = $uniqueName.'.'.$file->getClientOriginalExtension(); 
    $uploadSuccess = $file->move($destinationPath, $fileName); 
    $videoSrc = '/'.$destinationPath.'/'.$fileName; 

    $poster = $request->file('thumbnail'); 
    // Upload poster 
    $destinationPath = 'uploads/posters'; 
    $fileName = "poster".$uniqueName.'.'.$poster-  >getClientOriginalExtension(); 
    $uploadSuccess = $poster->move($destinationPath, $fileName); 
    $thumbnailSrc = '/'.$destinationPath.'/'.$fileName; 


    $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' => $videoSrc, 
     'file' => "file", 
     'thumbnail' => $thumbnailSrc, 
     'thumbnail' => "thumbnail", 
    ); 
    $i=DB::table('items')->insert($data); 
    if($i>0) 
    { 
     \Session::flash('message','new Item Saved'); 
     return redirect('itemindex'); 
    } 

(此代碼是你else語句)

+0

謝謝你。它的工作:D –

+0

當然是男人。我已經做了。並且非常感謝您的幫助。不斷傳播你的知識。 –