duan4523
2016-10-05 06:54
浏览 149
已采纳

在Laravel 5.1中上传视频和缩略图

I'm totally a newbie in Laravel development. I have a few text files along with 1 video upload and thumbnail upload feature. I can save all the data into the DB, but I am stuck with video and thumbnail/image upload.

Controller

<?php

public function save(Request $request)
{
    // Any other fields to be saved here..
    $post = $request->all();
    $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');
        }
    }
}

I added some code to test uploading images as a thumbnail but it failed.

View

<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>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dth2331 2016-10-05 09:51
    已采纳

    First you need to upload files(video/poster) then you save the uploaded path into your database.

    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');
        }
    

    (This code is for your else statement)

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题