Laravel Token Mismatch error during File Upload

I have been working on one of my side-projects in Laravel 4.2 with everything going smoothly until I hit an issue that I found absolutely frustrating. The project is a Web Content Management System with quite a good number of pages and each page from the CMS back-end contains at least 4 four file upload elements. As we progress in this blog post you will find out why I mentioned the upload elements. With every move I made, i felt even more frustrated because i did’t even know what exactly was the cause of the bug and it made resolving it hard.

The Issue

One of the CMS back-end page holds a form with 6 file upload elements. Each time I tried uploading images with all 6 of the file elements, Laravel threw a ‘Token Mismatch error‘. This error Laravel threw was a source of confusion because the token on this particular form matched with the application Session token so it made debugging quite difficult. Before i came to the source of this problem i tried so many things I read up from several Laravel help forums and blogs.

After days of no head way, I contacted a developer friend of mine and we took debugging of the issue back to basics. I began to upload files on the form one after another rather than trying to upload six at once. What we found was, the files uploaded until four but once it got past four uploads, Laravel screamed the ‘Token Mismatch error‘ .

After this session of debugging, it became clear this issue was from my local Wamp server. Down I went to the php.ini file of my Wamp server to inspect several properties associated with file upload and found a very interesting discovery.

 

The Solution

After playing with several properties associated with file upload, increased the ‘post_max_size‘ value in my Wamp server’s php.ini file, tried uploading  files from all six file elements on the troublesome form and…It Worked!.

According to php.net , post_max_size sets the maximum size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize. The php documentation for this property further states that “if the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals will be empty. So, there was my problem – the size of post data (post data in my own case was not just file elements but also text in textboxes and text areas) on that form was greater than the post_max_size value as specified in the php.ini file. Also, because my $_POST and $_FILES were empty, there was no token coming from the client form.  This obviously wasn’t going down well with Laravel as Laravel was comparing an empty token with a token set on my server hence the continuous ‘Token mismatch error‘. 

 

Conclusion

Laravel ‘Token mismatch error’ is mainly as a result of your form token not matching your Session token. However, sometimes Laravel could throw this error for several other reasons. So, if you ever face this annoying ‘Token Mismatch error‘ when trying to upload files,and are definitely sure your tokens match, I suggest you head to your php.ini file and have a closer look at those properties. You just might find the answer to your problem there.

Have you found any other reason for a Laravel ‘Token Mismatch error’ other that actual token mismatches? Please share.

Happy coding!

Share this: