php 如何使用 Laravel 上传视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40138976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to upload video with Laravel
提问by Vish_TS
I'm trying to upload a video while using Laravel. Though uploading images work fine for me when I change my controller line
我正在尝试在使用 Laravel 时上传视频。虽然当我更改控制器线路时上传图像对我来说很好用
echo '$file' . $file->getClientOriginalName() . '"/>';
to:
到:
echo '<file src="uploads/' . $file->getClientOriginalName() . '"/>';
I only see uploaded
written on a new page, but no video.
我只看到uploaded
写在新页面上,但没有视频。
Controller:
控制器:
<?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() . '"/>';
}
}
}
Routes:
路线:
Route::get('/', function () {
return view('welcome');
});
Route::post('upload', 'UploadController@upload');
View:
看法:
<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>
回答by Minhaj Mimo
You can try like this:
你可以这样试试:
Controller :
控制器 :
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 files contains some limits that might affect this. Try changing these to high enough values:
php.ini 文件包含一些可能会影响此的限制。尝试将这些更改为足够高的值:
upload_max_filesize = 10M
post_max_size = 10M
memory_limit = 32M
回答by Ahmed Mahmoud
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');
}
}