Search code examples
laravelfile-uploadlaravel-livewire

Laravel 9 Livewire The photo failed to upload


I started learning Laravel 9 livewire. I am trying to make an image upload application. I did the exact example in the Livewire documentation, but I get an error that the image could not be uploaded. When I do the same application on my mac computer, it works without any problems. It doesn't work when I do it on my windows computer.

ImageUpload.php

<?php

namespace App\Http\Livewire;

use Livewire\Component;
use Livewire\WithFileUploads;

class ImegaUpload extends Component
{
    use WithFileUploads;

    public $photo;

    public function save()
    {
        $this->validate([
            'photo' => 'image|max:1024', // 1MB Max
        ]);

        $this->photo->store('photos');
    }


    public function render()
    {
        return view('livewire.imega-upload');
    }
}

image-upload.blade.php


<div>
    <h1>Image upload</h1>
    <form wire:submit.prevent="save">
        <input type="file" wire:model="photo">

        @error('photo')
            <span class="error">{{ $message }}</span>
        @enderror

        <button type="submit">Save Photo</button>
    </form>
</div>

Error enter image description here

I'm doing the example in the Livewire documentation but still getting the error. Not even the livewire-temp folder is created.


Solution

  • Look like your PHP.ini is missing

    sys_temp_dir

    and

    upload_tmp_dir

    uncomment and then set its value.
    mine example.
    Laragon on win 10:

    sys_temp_dir = "c\laragon\tmp"

    upload_tmp_dir = "c\laragon\tmp"

    all temp files will go to that location (not just uploaded)