简体   繁体   中英

Upload Video and Thumbnail in 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>

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM