Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JacklynRoss's avatar

TokenMismatchException when using Laravel 5.0 FileSystem

I want to pull an image file from some folder, and I'm trying to use the extension method from the FileSystem class that i found in the Laravel Filesystem API in my controller, but I get a TokenMismatchException. This is what I have in my controller:

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Html\HtmlFacade;
use Illuminate\Filesystem\Filesystem;
use Filesystem;
use Request;
class PageController extends Controller{
    $image_extension = extension("C:/somepath/somefile");
    return view('display') ->with(compact('image_extension'));
}

What am I doing wrong?

0 likes
2 replies
davorminchorov's avatar

Is it really like that?

  1. you have to put the code inside a method which is also pointed in the routes.php file and when you get to that route (I am guessing POST), the code will execute.
  2. The method extension() should be on a Flysystem object, you can't call it like that.
public function store(Request $request, Flysystem $flysystem) 
{
    $image = $request->file('name');
    // you should probably move the uploaded file somewhere before you can access it. (I believe a good idea would be in the public directory, or to a cloud service like AWS S3)
    $imageExtension = $flysystem->extension('path/to/file');
    return view('display', compact('imageExtension'));
}

That should give you an idea.

Oh and what's your form like? Do you have:

<form>
    <input type="hidden" name="_token" value="{!! csrf_token() !!}"> 
</form>

in your blade file? It will generate the the token in the value attribute.

JacklynRoss's avatar

I'm taking an image from a folder defined in the routes folder:

define('Server_directory','C:/somepath/server_directory');

Simulates a folder on a server. Is there a better way to do this? I am trying to take an image from this folder and display it in my blade file.

Please or to participate in this conversation.