【发布时间】:2016-03-13 19:01:27
【问题描述】:
我在 laravel 5 中有一个上传表单,我想发送一个超过 2M 的文件,我将 php.ini 更改为最大上传大小和帖子大小,但它也不起作用! 当我的 csrf 启用时,我得到 csrf 令牌不匹配,当我在将表单中的所有字段发送到空控制器后禁用它时! 我的看法:
<div class="col-lg-6" style="float:right">
{!! Form::open(array('url'=>'admin/uploadtmp','method'=>'POST', 'files'=>true)) !!}
<div class="form-group col-md-12 pull-right">
{!! Form::text('title', Input::old('title'), array('class' => 'form-control', 'placeholder' => 'نام قالب')) !!}
<div style="color:red">
{!!$errors->first('title')!!}
</div>
</div>
<div class="form-group col-md-12">
{!! Form::textarea('description', Input::old('description'), array('class' => 'form-control', 'rows' =>'8', 'placeholder' => 'توضیحات')) !!}
<div style="color:red">
{!!$errors->first('description')!!}
</div>
</div>
<div class="form-group col-md-6" style="float:right">
<select name="brandid" class="form-control" placeholder="برند قالب">
<option value="">برند قالب خود را انتخاب کنید</option>
@foreach($brands as $brand)
<option value="{{$brand->id}} ">{{$brand->title}} </option>
@endforeach
</select>
</div>
<div class="form-group col-md-6" style="float:right">
<select name="categoryid" class="form-control">
<option value="">دسته بندی قالب خود را انتخاب کنید</option>
@foreach($categories as $category)
<option value="{{$category->id}} ">{{$category->title}} </option>
@endforeach
</select>
</div>
</div>
<div class="form-group col-md-6">
<div class="form-group">
{!! Form::label('لطفا فایل قالب خود را به صورت زیپ شده از طریق دکمه ی زیر انتخاب کنید') !!}
{!! Form::file('zip' , Input::old('zip')) !!}
<div style="color:red">
{!!$errors->first('zip')!!}
</div>
</div>
<div class="form-group">
{!! Form::label('لطفا فایل اسکرین شات قالب خود را با فرمت jpg و حجم کمتر از پنجاه کیلوبایت از طریق دکمه ی زیر انتخاب کنید') !!}
{!! Form::file('image') !!}
<div style="color:red">
{!!$errors->first('image')!!}
</div>
</div>
<input type="submit" name="submit" class="btn btn-primary pull-left" value="ارسال" >
{!! Form::close() !!}
@if(Session::has('error'))
<div class="col col-lg-12 alert alert-success alert-dismissable">
<button style="float:left" type="button" class="close" data-dismiss="alert" aria-hidden="true">× </button>
{!! Session::get('error') !!}
</div>
@endif
@if(Session::has('success'))
<div class="col col-lg-12 alert alert-success alert-dismissable">
<button style="float:left" type="button" class="close" data-dismiss="alert" aria-hidden="true"> × </button>
{!! Session::get('success') !!}
</div>
@endif
</div>
</div>
我的控制器:
class TemplateController extends Controller
{
public function newtemplate(){
// getting all of the post data
$files = array(
'zip' => Input::file('zip'),
'image' => Input::file('image'),
'title' => Input::input('title'),
'description' => Input::input('description'),
'brandid' =>Input::input('brandid'),
'categoryid' =>Input::input('categoryid'),
);
$rules = array(
'image' => 'mimes:jpeg,jpg | max:5000 | required',
'zip' => 'mimes:zip,rar | max:40000 | required',
'title' => 'required',
'description' => 'required',
); //mimes:jpeg,bmp,png and for max size max:10000
$messages =[
'image.mimes' => 'فرمت فایل ارسالی شما صحیح نمی باشد',
'image.max' => 'حداکثر اندازه ی فایل ارسالی شما باید 50 کیلو بایت باشد',
'zip.mimes' => 'فرمت فایل ارسالی شما باید حتما zip باشد',
'required' => 'این فیلد نباید خالی باشد'
];
$validator = Validator::make($files, $rules,$messages);
if ($validator->fails()) {
// send back to the page with the input data and errors
return Redirect::to('admin/templates')->withInput()->withErrors($validator);
}
else{
if (Input::file('zip')->isValid() && Input::file('image')->isValid()) {
$template = new Template;
$template->creator_id = Auth::user()->id;
$template->title = Input::input('title');
$template->description = Input::input('description');
$template->category_id = Input::input('categoryid');
$template->brand_id = Input::input('brandid');
$template->save();
$lastid = $template->id;
//$destinationPath = 'uploads'; // upload path
$extension = Input::file('zip')->getClientOriginalExtension(); // getting image extension
$extension2 = Input::file('image')->getClientOriginalExtension(); // getting image extension
$filename = $lastid.'.'.$extension; // renameing image
$filename2 = $lastid.'.'.$extension2; // renameing image
//Storage::disk('local')->put('screenshots/'.$filename, Input::file('image'));
Input::file('zip')->move(storage_path().'/zipfiles', $filename);
Input::file('image')->move(storage_path().'/screenshots', $filename2);
//Storage::disk('local')->put('zipfiles/'.$filename2,Input::file('zip'));
// sending back with message
Session::flash('success', 'عملیات با موفقیت صورت گرفت');
return Redirect::back();
}
else {
// sending back with error message.
Session::flash('error', 'مشکل در آپلود فایل ها');
return Redirect::back();
}
}
}
}
【问题讨论】:
-
您是否通过 ajax 调用发送数据?
-
您在更改 php.ini 设置后是否重新启动了 apache/您的网络服务器?
-
你使用的是 Apache 还是 Nginx?
-
我在 win 7 上使用 wamp 服务器,是的,我在更改后重新启动它!我使用带有 post 方法的表单发送数据并且我没有使用 ajax,它适用于大小小于 2M 的文件!!!
-
我想知道,它看起来像服务器问题。可以肯定的是,尝试制作一个虚拟 php 脚本来转储您的输入,例如
<?php var_dump($_POST) ?>在某个其他目录中,然后将您的表单传递给它。另外,如果可能的话-澄清一下,我们可以看看你的upload_max_filesize和post_max_size吗?
标签: laravel upload csrf-protection large-file-upload