2016-10-05 144 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

当然是男人。我已经做了。并且非常感谢您的帮助。不断传播你的知识。 –