如何使用Laravel上传视频

时间:2016-10-19 18:27:28

标签: php mysql laravel post

我在尝试使用Laravel时上传视频。虽然在我更改控制器线时上传图像对我来说很好用

echo '$file' . $file->getClientOriginalName() . '"/>';

为:

echo '<file src="uploads/' . $file->getClientOriginalName() . '"/>';

我只看到uploaded写在新页面上,但没有视频。

控制器:

<?php
namespace App\Http\Controllers;

use Illuminate\Support\Facades\Request;


class UploadController extends Controller
{

    public function upload(Request $request)
    {

        if(Request::hasFile('file')){

            echo 'Uploaded';
            $file = Request::file('file');
            $file->move('uploads', $file->getClientOriginalName());
            echo '$file' . $file->getClientOriginalName() . '"/>';
        }

    }
}

路线:

Route::get('/', function () {
    return view('welcome');
});

Route::post('upload', 'UploadController@upload');

查看:

<html>
    <head>
        <title>Laravel</title>

        <link href='//fonts.googleapis.com/css?family=Lato:100' rel='stylesheet' type='text/css'>

    </head>
    <body>
        <div class="container">
            <div class="content">

                <h1>File Upload</h1>
                <form action="{{ URL::to('upload') }}" method="post" enctype="multipart/form-data">
                    <label>Select image to upload:</label>
                    <input type="file" name="file" id="file">
                    <input type="submit" value="Upload" name="submit">
                    <input type="hidden" value="{{ csrf_token() }}" name="_token">
                </form>

            </div>
        </div>
    </body>
</html>

3 个答案:

答案 0 :(得分:5)

您可以尝试这样:

控制器:     

use Illuminate\Support\Facades\Request;


class UploadController extends Controller
{

    public function upload(Request $request)
    {

        if(Request::hasFile('file')){

            $file = Request::file('file');
            $filename = $file->getClientOriginalName();
            $path = public_path().'/uploads/';
            return $file->move($path, $filename);
        }

    }
}

php.ini文件包含一些可能会影响此问题的限制。尝试将这些值更改为足够高的值:

upload_max_filesize = 10M
post_max_size = 10M
memory_limit = 32M

答案 1 :(得分:1)

 public function upload_video(Request $request){ 
             $data=$request->all();
              $rules=[
                'video'          =>'mimes:mpeg,ogg,mp4,webm,3gp,mov,flv,avi,wmv,ts|max:100040|required'];
             $validator = Validator($data,$rules);

             if ($validator->fails()){
                 return redirect()
                             ->back()
                             ->withErrors($validator)
                             ->withInput();
             }else{
                        $video=$data['video'];
                        $input = time().$video->getClientOriginalExtension();
                        $destinationPath = 'uploads/videos';
                        $video->move($destinationPath, $input);

                            $user['video']       =$input;
                            $user['created_at']  =date('Y-m-d h:i:s');
                            $user['updated_at']  =date('Y-m-d h:i:s');
                            $user['user_id']     =session('user_id');
                            DB::table('user_videos')->insert($user);
                            return redirect()->back()->with('upload_success','upload_success');
                    }
}

答案 2 :(得分:0)

if(isset($image)) { if($image->getClientOriginalExtension()=='gif'||$image->getClientOriginalExtension()=='svg' ||$image->getClientOriginalExtension()=='mp4' || $image->getClientOriginalExtension( )=='avi'){

        $image = $request->file('image');
        $extension = $image->getClientOriginalExtension();
        $name = $image->getClientOriginalName();
        $fileName = 'exerciseimages'."-".$name;
        $image->move('storage/courseimages/',$fileName);
    }else{
         

        $fileName = 'exerciseimages'.'-'.uniqid().'.'.$image->getClientOriginalExtension();
    
        if(!Storage::disk('public')->exists('courseimages')){
            Storage::disk('public')->makeDirectory('courseimages');
        }
        $amenitiesimg = Image::make($image)->resize(250,250)->stream();
        Storage::disk('public')->put('courseimages/'.$fileName, $amenitiesimg);

       
    }