多文件上传的Laravel 5 TokenMismatchException

Sai*_*nce 3 php file-upload exception laravel-5

在我的Laravel 5应用程序中,管理员可以上传产品图像和产品的pdf文件.因此,表单有2个输入字段,如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

当我上传小于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
Run Code Online (Sandbox Code Playgroud)

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

  1. 图片:名称:flower-1.jpg,尺寸: 51.6kb
  2. PDF:名称:productInfo.pdf,大小: 777.2kB

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

  1. 图片:名称:flower-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);
}
Run Code Online (Sandbox Code Playgroud)

编辑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>
Run Code Online (Sandbox Code Playgroud)

编辑2: 仅供参考,我在Ubuntu 14.04 LTS x64位架构上使用LAMP.这是一个本地主机.我尚未托管该应用程序.

请帮助我.谢谢.

Web*_*ner 5

我有同样的问题,并且能够通过增加UPLOAD_MAX_FILESIZE和POST_MAX_SIZE PHP设置来解决它.前者应该大于您上传的单个文件,后者应该大于上传的两个(或更多)文件的总和.

有一个更好的解释,它对$ _POST变量的作用是什么导致这个呈现为令牌不匹配异常:

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

希望如果你还没有解决这个问题,这对你有用!