Josh1:9's avatar

Uploading a video file above 2MB throws TokenMismatch Exception on Laravel 5.3.22

Hi everyone, I have been trying to upload a video through a form on my Laravel application but it has been difficult.

I have csrf token on my form and still when I try to upload a video above 2MB the form throws up tokenMismatch Exception.

When I try to upload either an image file or a video file less than 2MB it works fine. But immediately I try to upload a video file more than that, it throws up the Exception.

I have increased my post_max_size and upload_max_filesize in php.ini to a value far above that, restarted my machine/web server severally and still experience the same issues.

I am working with Laravel 5.3.22 on a MAC OS with PHP 7 on XAMPP

Below is some part of my file upload form and processing controller

//Form opening tag
{{Form::open(['url'=>'/admin/lesson','class'=>'form-horizontal ','method'=>'POST','enctype'=>'multipart/form-data'])}}

and the line for the file

        <div class="form-group{{ $errors->has('lesson_file') ? ' has-error' : '' }}">
                            <label for="lesson-file" class="col-md-4 control-label">Lesson File</label>

                            <div class="col-md-6">
                                {{Form::file('lesson_file',['class'=>'form-control','id'=>'lesson-file'])}}
                               
                                @if ($errors->has('lesson_file'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('lesson_file') }}</strong>
                                </span>
                                @endif
                            </div>
                        </div>

I'll appreciate any guide to solve this.

Thank you.

0 likes
15 replies
ohffs's avatar

Are you sure you've updated the right php.ini?

Josh1:9's avatar

@ohffs, Yes I am sure. I have checked my php_info() and it shows the new values

ohffs's avatar

Ah - not sure then I'm afraid. Any time I've seen the same thing happen it's been php.ini :-/

SaeedPrez's avatar

How big is the file? Wouldn't hurt to try also changing max_execution_time

Josh1:9's avatar

@SaeedPrez, they are in the range of 20MB. I can increase max_execution time but my problem is that the error doesn't show that the problem is related to filesize. It's rather showing that its csrf token mismatch whereas csrf token is actually in place on the form when i check the page source.

SaeedPrez's avatar

@Josh1:9 you got a point, make sure your post_max_size is larger (i.e. 2x as large) than your upload_max_filesize and that memory_limit is larger than post_max_size

Josh1:9's avatar

@SaeedPrez, Thank you for your suggestions. However, I have done all that you suggested

I have made upload_max_filesize=200M post_max_size=400M memory_limit=500M max_exec_time=60

but the problem persists.

SaeedPrez's avatar

@Josh1:9 I'm running out of ideas, what about the max_input_time ? Try change it to something higher as well... other than this, only thing I can think of is that it's the wrong php.ini .. hm..

martinbean's avatar

@Josh1:9 Web applications aren’t meant to upload large files in a single transaction. You’ll need to chuck it and upload the file in parts. There are lots of JavaScript libraries to help you achieve this, such as https://blueimp.github.io/jQuery-File-Upload/ and AWS’s JavaScript SDK if you’re trying to upload to S3.

Josh1:9's avatar

@SaeedPrez, I have done all that all to no avail. I think something else must be the problem somewhere.

I cannot say why its complaining of csrf token mismatch

mdecooman's avatar

Hi Josh1:9,

Seems someone had already this issue: https://laravel.io/forum/02-20-2014-l40-csrf-tokenmismatchexception-when-uploading-large-files

if you upload more then your server's post_max_size setting the input will be empty, hence a token miss-match so if you have 2 upload input's in your form and your upload_max_filesize is 2G, your post_max_size needs to be at least 4G

hope it helps

PS: also make sure you restart your php-fpm after changing the values.

shez1983's avatar

nginx (or apache) also has limits btw.. just a FYI -

Please or to participate in this conversation.