2015-07-11 150 views
3

在我Laravel 5的应用,存在用于管理员上传的产品形象和产品的PDF文件制作。所以,形式有2个输入字段这样的:Laravel 5 TokenMismatchException对多文件上传

<div class="col-md-4 col-sm-6"> 
    <div class="form-group"> 
     {!! Form::label('image', 'Image File:') !!} 
     {!! Form::file('image', ['class' => 'form-control input-sm']) !!} 
    </div> 
</div> 

<div class="col-md-4 col-sm-6"> 
    <div class="form-group"> 
     {!! Form::label('leaflet', 'Leaflet:') !!} 
     {!! Form::file('leaflet', ['class' => 'form-control input-sm']) !!} 
    </div> 
</div> 

当我上载超过2MB的图像和单张都不到,它就会被上传成功。但在使用时,小叶大于2MB,我得到TokenMismatchException at line 46

在我php.ini文件,该文件位于/etc/php5/apache2/php.ini我的配置如下所示:

; Maximum allowed size for uploaded files. 
; http://php.net/upload-max-filesize 
upload_max_filesize = 2G 

; Maximum size of POST data that PHP will accept. 
; Its value may be 0 to disable the limit. It is ignored if POST data reading 
; is disabled through enable_post_data_reading. 
; http://php.net/post-max-size 
post_max_size = 6G 

,我上传的是文件(工作):

  1. 图片:名称:花1.JPG,尺寸:51.6kb
  2. PDF:名称:productInfo.pdf,尺寸: 777.2kB

,我上传的文件(不工作 - 在VerifyCsrfToken.php在第46行给出TokenMismatchException):

  1. 图片:名称:花1.JPG,尺寸:51.6kb
  2. PDF:名称:productInfo-次数1.pdf,尺寸:5.00MB

控制器

public function update($id, UpdateProductRequest $request) { 
    $product = $this->prod->findProductById($id); 

    $this->prod->updateProductOfId($product->id, $request); 

    Flash::success('product_general_info_updated', 'The product general information has been successfully updated.'); 

    return redirect()->back(); 
} 

/** 
* Coming from ProductRespository.php 
*/ 
public function updateProductOfId($id, Request $request) 
{ 
    $prd = $this->findProductById($id); 

    $getAllInput = $request->all(); 

    if($request->file('image')) 
    { 
     $imageType = array(
      'product' => array(
       'height' => 347, 
       'width' => 347 
      ), 
      'category' => array(
       'height' => 190, 
       'width' => 190 
      ) 
     ); 

     $imageFileName = $request->file('image')->getClientOriginalName(); 

     foreach ($imageType as $key => $value) 
     { 
      $currentFile = Input::file('image'); 
      $fileName = $currentFile->getClientOriginalName(); 
      $image = Image::make($request->file('image')); 
      $name = explode('.', $fileName); 
      $destinationPath = public_path().'/images/products/uploads'; 
      if ($key === 'product') { 
       $image->resize($value[ 'width' ], $value[ 'height' ]); 
       $image->save($destinationPath . '/' . $name[ 0 ] . "-" . $value[ 'width' ] . "-" . $value[ 'height' ] . ".jpg", 100); 
      } elseif ($key === 'category') { 
       $image->resize($value[ 'width' ], $value[ 'height' ]); 
       $image->save($destinationPath . '/' . $name[ 0 ] . "-" . $value[ 'width' ] . "-" . $value[ 'height' ] . ".jpg", 100); 
      } 
     } 
     $getAllInput['image'] = $imageFileName; 
    } 

    if($request->file('leaflet')) 
    { 
     $currentFile = Input::file('leaflet'); 
     $fileName = $currentFile->getClientOriginalName(); 
     $destinationPath = public_path().'/leaflets/products/uploads'; 

     $currentFile->move($destinationPath, $fileName); 
     $getAllInput['leaflet'] = $fileName; 
    } 
    return $prd->update($getAllInput); 
} 

编辑1: 我使用Form Model Binding,这样既createedit文件具有相同的形式:

<div class="container"> 
    @include('errors.list') 

    {!! Form::open(['url' => '/admin/products', 'autocomplete' => 'off', 'files' => true]) !!} 
     @include('admin.products.product_general_form', ['submitButtonText' => 'Add Product']) 
    {!! Form::close() !!} 
</div> 

编辑2: 只是为了信息,我在Ubuntu 14.04 LTS x64位架构上使用LAMP。这是一个本地主机。我还没有托管该应用程序。

请帮助我。谢谢。

+0

您可以在您的视图中打开表单的位置添加代码吗? –

+0

您是否使用Former处理您的表单?我无法理解你如何将文件上传到2 MB,但不能再更新。你在这里使用什么网络服务器? (抱歉回答迟了) –

+0

您是否尝试过类似'ini_get('upload_max_filesize')'来检查您的最大上传文件大小是否已由conf文件正确设置? –

回答

-3

添加{! csrf_token()!}表单中生成一个CSRF令牌。

{!! Form::open(['url' => '/admin/products', 'autocomplete' => 'off', 'files' => true]) !!} @include('admin.products.product_general_form', ['submitButtonText' => 'Add Product']) <input type="hidden" name="_token" value="{!! csrf_token() !!}"> {!! Form::close() !!}

目前提交您的形式,没有设置CSRF令牌,该令牌Laravel询问,因为VerifyCsrfToken.php中间件。

+0

谢谢您的输入,先生,但我会请你再读一遍我的问题。因为我已经清楚地表明,低于2MB的文件正在上传,而大于此值则抛出'TokenMismatchException'。仅仅为了您的信息,HTML Form Facade默认包含'_token'。 –

+0

您是否增加了php配置中的max_execution_time? –

+0

为什么你认为我需要在我的PHP配置中增加max_execution_time? –

5

我有同样的问题,并能够通过增加的upload_max_filesize和post_max_size要PHP设置来解决这个问题。前者应大于上传的单个文件,后者应大于正在上传的两个(或更多)文件的总数。

有什么这样做的$ _POST变量导致这表现为一个令牌不匹配异常这里更好的解释:

http://laravel.io/forum/02-20-2014-l40-csrf-tokenmismatchexception-when-uploading-large-files

希望这对你的作品,如果你还没有解决这已经!

+0

POST_MAX_SIZE是我的罪魁祸首。 –