【发布时间】:2016-07-27 11:28:35
【问题描述】:
我正在尝试使用 Laravel 5.2
通过表单上传视频和图像当只上传图片时,它可以正常工作,但是当我尝试上传视频时,它只会抛出 TokenMismatchException in VerifyCsrfToken.php line 67 错误。
我的表单确实有隐藏的 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');
});
控制器 store() 方法:
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;
}
到目前为止,我还没有进行任何验证,所以当我尝试在没有视频的情况下上传时,它可以工作并保存到文件夹中,并将文件夹路径输出到数据库中。但是,当我尝试用它上传视频时,它会引发错误。
我有点卡在这里,有人有什么想法吗?
【问题讨论】:
-
您是否在 .env 文件中生成并保存了唯一的 APP_KEY?也试试 php artisan dump-autoload
-
你的 config/session.php
driver值是多少?如果是文件,请确保同一文件中的files具有正确的路径, -
是的 app_key 是在 .env 文件中生成的并且转储 autoload 没有任何东西
-
什么是文件的正确 pat?
-
'files' => storage_path('framework/sessions'),
标签: php laravel laravel-5.2