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

medusha's avatar

Having trouble with uploading file in Laravel+inertia

I have a form that takes in one text input and a file input. The text inputs are working but the file inputs are show null. In the form page, i have this:

    const { data, setData, post, processing, reset, errors, progress } = useForm({
        title: '', 
        pdf_file: null,
    });
    const submit = (e) => {
        e.preventDefault();
        const formData = new FormData();
        formData.append('title', data.title);
        formData.append('pdf_file', data.pdf_file);
        post(route('chirps.store'), { 
            data: formData, 
            onSuccess: () => reset() 
          });
        };

And the form is like this:

                <form onSubmit={submit} enctype="multipart/form-data">
                    <input type="text" name="title" placeholder="Title"  onChange={e => setData('title', e.target.value)}  />
                    <input type="file"  name="pdf_file" onChange={e => set('pdf_file', e.target.files[0])} />
                    <div><PrimaryButton className="mt-4" disabled={processing}>Post Note</PrimaryButton></div>
                </form>

In the controller file, I have this for the "chirps.store" function:

    public function store(Request $request): RedirectResponse
    {
        $validated = $request->validate([
            'title' => 'required|string|max:69', 
            'pdf_file' => 'nullable|mimes:pdf|max:2048',
        ]);

        if($request->hasFile('pdf_file')){
            $pdf_file = $request->pdf_file;
            $pdf_file_name = time().'_'.$pdf_file->getClientOriginalName();
            $pdf_file->move(public_path('pdf'), $pdf_file_name);
            $pdf_file = $pdf_file_name;
        } else {
            $pdf_file = "not found";
        }

        $validated['pdf_file'] = $pdf_file;

        $request->user()->chirps()->create($validated);
        return redirect(route('chirps.index'));
    }

This is not working as i'm getting pdf_file=not found in my database. Can someone help

0 likes
1 reply
medusha's avatar
medusha
OP
Best Answer
Level 1

I found the problem. I changed "set" to "setData" in the input tag and my "public" folder didn't have the permission to write by others. I reset the permission and it worked.

Please or to participate in this conversation.