Laravel 5 TokenMismatchException when uploading multiple files
My Laravel 5 application stipulates that an admin uploads a product image and a product PDF file. So the form has 2 input fields like this:
<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>
When I upload the image and brochure as less than 2MB, it uploads successfully. But when using flyer more than 2MB I getTokenMismatchException at line 46
In my file php.ini
that is in /etc/php5/apache2/php.ini
I have a configuration like this:
; 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
The files I download ( work ):
- Image: Name: flower-1.jpg, Size:
51.6kb
- PDF: Name: productInfo.pdf, Size:
777.2kB
The files I upload ( doesn't work - throws a TokenMismatchException on line 46 in VerifyCsrfToken.php ):
- Image: Name: flower-1.jpg, Size:
51.6kb
- PDF: Name: productInfo-1.pdf, Size:
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);
}
Edit 1:
I am using Form Model Binding
, so both files create
and edit
have the same shape:
<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>
EDIT 2: Just for information, I am using LAMP on Ubuntu 14.04 LTS x64-bit architecture. This is localhost. I haven't posted the app yet.
Please help me. Thank.
source to share
I had the same problem and was able to solve it by increasing the PHP UPLOAD_MAX_FILESIZE and POST_MAX_SIZE settings. The former must be larger than the individual files you are downloading, and the latter must be larger than the sum of the two (or more) files that you are downloading.
There's a better explanation of what it does with the $ _POST variable which results in this view as a token mismatch exception here:
http://laravel.io/forum/02-20-2014-l40-csrf-tokenmismatchexception-when-uploading-large-files
Hope this works for you if you haven't solved it yet!
source to share
Add {!! csrf_token () !!} in your form to generate a CSRF token.
{!! 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() !!}
There is currently no CSRF token provided by Laravel when submitting your form due to VerifyCsrfToken.php .
source to share