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

时间:2016-10-05 06:54:34

标签: php laravel laravel-5 laravel-5.1

我完全是Laravel开发的新手。我有一些文本文件以及1个视频上传和缩略图上传功能。我可以将所有数据保存到数据库中,但我坚持使用视频和缩略图/图像上传。

控制器

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

我添加了一些代码来测试上传图片作为缩略图,但它失败了。

查看

<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 个答案:

答案 0 :(得分: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');
    }

(此代码适用于您的其他声明)