1

I'm trying to implement this guide on how to upload an image to the laravel storage but when I submit, it shows that the page is expired. There is not error report in the log which makes it difficult to debug.

web.php:

Route::get('/upload-image', [StorageController::class, 'index']);
Route::post('/save', [StorageController::class, 'save']);

StorageController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Image;
use App\Models\Photo;



class StorageController extends Controller
{
    public function index()
    {
        return view('image');
    }

    public function save(Request $request)
    {
        $validatedData = $request->validate([
            'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048',
        ]);

        $name = $request->file('image')->getClientOriginalName();
        $path = $request->file('image')->store('public');

        $save = new Photo;
        $save->name = $name;
        $save->path = $path;
        $save->save();

        return redirect('upload-image')->with('status', 'Image Has been uploaded');
    }
}

Model Photo.php:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Photo extends Model
{
    use HasFactory;
}

Laravel view to upload the image image.blade.php:

<!DOCTYPE html>
<html>
<head>
  <title>Laravel 8 Uploading Image</title>
  <meta name="csrf-token" content="{{ csrf_token() }}">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
  <div class="container mt-5">
    @if(session('status'))
      <div class="alert alert-success">
        {{ session('status') }}
      </div>
    @endif
    <div class="card">
      <div class="card-header text-center font-weight-bold">
        <h2>Laravel 8 Upload Image Tutorial</h2>
      </div>
      <div class="card-body">
        <form method="POST" enctype="multipart/form-data" id="upload-image" action="{{ url('/save') }}" >
          <div class="row">
            <div class="col-md-12">
              <div class="form-group">
                <input type="file" name="image" placeholder="Choose image" id="image">
                @error('image')
                <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
                @enderror
              </div>
            </div>
            <div class="col-md-12">
              <button type="submit" class="btn btn-primary" id="submit">Submit</button>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</body>
</html>

So when I navigate to localhost/upload-image it shows the view and I can choose a file in the input form but as soon as I click on the submit button, the page navigates to /save and shows 419 | Page Expired with no log entry. The browser console shows:

POST http://127.0.0.1:8000/save 419 (unknown status)
1
  • Can you please pass in request csrf <form method="POST" enctype="multipart/form-data" id="upload-image" action="{{ url('/save') }}" > @csrf Commented Mar 11, 2022 at 7:21

2 Answers 2

3

You are not passing @csrf token in form request:

<form method="POST" enctype="multipart/form-data" id="upload-image" action="{{ url('/save') }}" >
@csrf

Please pass as per above code example then it will be work.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Now the controller uploads the image into the storage as well as creating an entry in the database
0

You should include @csrf in your form tags.

<form method="POST" enctype="multipart/form-data" id="upload-image" action="{{ url('/save') }}" >
@csrf
</form>

1 Comment

This answer was already given…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.