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)

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?