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

jasonmccallister's avatar

CSRF issue when trying to upload a video?

I'm using the Vimeo API wrapper from https://github.com/vinkla/vimeo.

When trying to use the facade Vimeo to upload the file (from the storage path) I am receiving this error:

TokenMismatchException in compiled.php line 2387:

My controller is rather simple, you can view the full controller here: http://laravel.io/bin/Lk3d9.

public function upload(UploadVideoFormRequest $request)
    {
        $directory = storage_path('tmp/' . md5(time()));
 
        $video = $request->file('video');
        // dd($video);
 
        $filename = 'video' . '.' . $video->getClientOriginalExtension();
        // dd($filename);
 
        $video->move($directory, $filename);
 
        $this->uploadVideo($directory, $filename);
 
        return redirect()->back();
    }
 
    protected function uploadVideo($directory, $filename)
    {
        $video = $directory . $filename;
 
        Vimeo::upload($video, false);
 
        return $this;
    }

The local upload works fine. I can see and view the video in my local storage. However, as soon as I pass the information off to the Vimeo service provider, it throws that error, any ideas?

0 likes
4 replies
jasonmccallister's avatar

Also, the token is exactly the same on refresh... I don't recall that before. It is different per browser though.

jasonmccallister's avatar

Ok, so I am going to post my full code here. Moved the code into a whole new Laravel 5 installation and I am still getting the same issue.

HTML Form:

{!! Form::open(['route' => ['projects_videos_update', $project->id], 'method' => 'post', 'files' => true]) !!}
        <h2>Add a Video</h2>
        <p>Select your video.</p>
        <div class="field file">
            <span class="label offleft">
                <label for="videoField">Video</label>
            </span>
            {!! Form::file('video', ['class' => 'video', 'id' => 'videoField']) !!}
        </div> <!-- /.field -->
        <div class="field select">
            <span class="label">
                <label for="videoCategory">Category</label>
            </span>

                    <span class="selectWrap">
                        <select name="category" id="videoCategory">
                            @foreach($categories as $category)
                                <option value="{{ $category->id }}">{{ $category->name }}</option>
                            @endforeach
                        </select>
                    </span>
        </div> <!-- /.field -->
        <p>
            <button type="submit" class="button">Add Video</button>
            <button type="button" class="button danger cancel">Cancel</button>
        </p>
        {!! Form::close() !!}

Routes.php

Route::get('projects/{id}/videos', ['as' => 'projects_videos', 'uses' => 'VideoController@show']);
Route::post('projects/{id}/videos', ['as' => 'projects_videos_update', 'uses' => 'VideoController@upload']);

And finally the controller:

public function upload(Request $request, $id)
    {
        // set the storage directory
        $directory = storage_path('videos/temp' . md5(now()));

        if (Video::where('project_id', $id)->first() == null) {
            $video = new Video;
        }
        else {
            $video = Video::where('project_id', $id)->first();
        }

        $upload = $request->file('video');

        $filename = 'video' . '.' . $upload->getClientOriginalExtension();

        $upload->move($directory, $filename);

        $video->storage_path = $directory . $filename;

        $project = Project::find($id);
        $video->project()->associate($project);

        $video->save();

        // $this->vimeo->upload($directory . $filename, false);

        return redirect()->back();
    }

Please or to participate in this conversation.