我正在尝试使用 Laravel 5.2
通过表单上传视频和图片上传图片时,它没有任何问题,但是当我尝试上传视频时,它只会在VerifyCsrfToken.php第67行错误中抛出 TokenMismatchException。
我的表单确实有隐藏的csrf_field {!! csrf_field()!!} ,我的路线在 Route :: group([' middleware' => [' web']],function() {}
我无法弄清楚为什么这不起作用。
形式:
<form action="{{ url('admin-backend/video') }}" method="POST" enctype="multipart/form-data">
{!! csrf_field() !!}
<input type="text" class="form-control" name="name" placeholder="Name">
<textarea name="description" class="form-control" rows="8"></textarea>
<input type="submit" value="submit">
<input type="file" class="form-control" name="video" />
<input type="file" class="form-control" name="feature_image" />
<input type="file" class="form-control" name="image_1" />
<input type="file" class="form-control" name="image_2" />
<input type="file" class="form-control" name="image_3" />
<input type="file" class="form-control" name="image_4" />
<input type="file" class="form-control" name="image_5" />
<input type="file" class="form-control" name="image_6" />
</form>
路线:
Route::group(['middleware' => ['web']], function () {
Route::get('/admin-backend', function () {
return view('backend.admin.index');
});
Route::get('/admin-backend/video', 'adminVideoController@index');
Route::get('/admin-backend/video/create', 'adminVideoController@create');
Route::post('/admin-backend/video', 'adminVideoController@store');
Route::get('/admin-backend/video/{id}', 'adminVideoController@show');
Route::get('/admin-backend/video/{id}/edit', 'adminVideoController@edit');
Route::put('/admin-backend/video/{id}', 'adminVideoController@update');
});
控制器存储()方法:
public function store(Request $request)
{
$fields = Video::prepareVideoUpload($request);
$video = Video::create($fields);
return view('backend.admin.videos.create');
}
视频模型:
protected $table = 'videos';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name','description','feature_image','image_1','image_2','image_3','image_4','image_5','image_6','video','created_at','updated_at'
];
/**
* @param $request
* @return array
*
* This function prepares the video upload by placing all relevant data into the $fields array.
* It creates the necessary folder structure to place the images and video for each shoot
*
*/
public static function prepareVideoUpload($request)
{
$fields = []; //This will be used to store the information in the database instead of request
$fields['name'] = $request['name'];
$fields['description'] = $request['description'];
$videoPath = public_path() . '/videos/' . substr(date("Y/m/d"),0,7); // videoPath looks like - /Applications/MAMP/htdocs/website/public/videos/2016/04
$name = str_replace(' ', '-', $request['name']);
$newVideoPath = $videoPath . '/' . $name;
//if the folder is already created just make the new video name folder
if( is_dir( $videoPath ) ) {
mkdir($newVideoPath);
}else{
//create the folder structure /year/month/video-name
mkdir($newVideoPath, 0777, true);
}
//Create the video and images folders for the individual shoot
mkdir($newVideoPath . '/video');
mkdir($newVideoPath . '/images');
//If the video was uploaded successfully, move it to the images directory
if ($request->hasFile('video') && $request->file('video')->isValid()) {
$request->file('video')->move($newVideoPath . '/video',$request->file('video')->getClientOriginalName());
$fields['video'] = 'videos/' . substr(date("Y/m/d"),0,7) . '/' . $name . '/video/' . $request->file('video')->getClientOriginalName();
}
//If the feature image was uploaded successfully, move it to the images directory
if ($request->hasFile('feature_image') && $request->file('feature_image')->isValid()) {
$request->file('feature_image')->move($newVideoPath . '/images',$request->file('feature_image')->getClientOriginalName());
$fields['feature_image'] = 'videos/' . substr(date("Y/m/d"),0,7) . '/' . $name . '/images/' . $request->file('feature_image')->getClientOriginalName();
}
for($i=1;$i < 7; $i++){
//If the image was uploaded successfully, move it to the images directory
if ($request->hasFile('image_' . $i) && $request->file('image_' . $i)->isValid()) {
$request->file('image_' . $i)->move($newVideoPath . '/images',$request->file('image_' . $i)->getClientOriginalName());
$fields['image_' . $i] = 'videos/' . substr(date("Y/m/d"),0,7) . '/' . $name . '/images/' . $request->file('image_' . $i)->getClientOriginalName();
}
}
return $fields;
}
我还没有完成任何验证,所以当我尝试上传没有视频时,它会工作并保存到文件夹并将文件夹路径输出到数据库中。但是,当我尝试使用它上传视频时,它会抛出错误。
我有点卡在这里,有人有任何想法吗?
答案 0 :(得分:2)
我的猜测是你在POST数据上达到最大大小限制,导致它只是丢弃输入。
您可以将您的网络服务器和php调整到更高的限制,看看它是否有帮助。
php.ini中的 post_max_size
将是一个很好的起点。