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

在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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功