php - 多个文件上传时的 Laravel 5 TokenMismatchException

标签 php exception file-upload 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>

当我上传小于2MB的图片和传单时,就成功上传了。但是在使用时,传单超过 2MB,我在第 46 行得到 TokenMismatchException

在位于 /etc/php5/apache2/php.iniphp.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. 图片:名称: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

Controller

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。这是一个本地主机。我还没有托管该应用程序。

请帮帮我。谢谢。

最佳答案

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

关于这对 $_POST 变量的作用有更好的解释,这会导致此处显示为 token 不匹配异常:

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

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

关于php - 多个文件上传时的 Laravel 5 TokenMismatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31354353/

相关文章:

php - 使用 SYmfony 在 PHP 中处理 "Undefined Index"

c# - 无法从 'List<string>' 转换为 'string' 异常

javascript - 上传并保存从 django --canvas 元素中的用户网络摄像头获取的屏幕截图到文件

php - mysql 将 3 个查询合并为 3 个类别的 SUM

php - PHP镜像未显示

php - 在 php 中连接到 Linux 上的 MS SQL Server

asp.net - ASPX 服务器异常

php - 如何在 PHP 中启用跨域 POST-ing?

Ruby on Rails 上传文件问题 奇数 utf8 转换错误

javascript - 使用phonegap将图像上传到网络服务器的问题