el_maccho's avatar

Laravel 11 + react inertia problem with image upload

Hello, after sending the form, the photo is saved in the storage/app/ folder, but in the database it is saved as a system path (C:/user/appdata/local/temp etc...). How to fix it? when trying to display a photo on the website, I get an error: Not allowed to load local resource

export default function PersonalInfo({ mustVerifyEmail, status, progress }) { const user = usePage().props.auth.user;

const { data, setData, post, errors, processing, recentlySuccessful } =
    useForm({
        name: user.name,
        surname: user.surname,
        email: user.email,
        address: user.address,
        phone_number: user.phone_number,
        profile_image_path: user.profile_image_path,
    });

const submit = (e) => {
    e.preventDefault();

    post(route("profile.update"));
};

return (

                    <label
                        htmlFor="image_picker"
                        className="image-picker-label"
                    >
                        <p>Wybierz zdjęcie profilowe </p>
                        <FontAwesomeIcon icon={faImage} />
                    </label>
                    <input
                        type="file"
                        id="image_picker"
                        hidden
                        onChange={(e) =>
                            setData("profile_image_path", e.target.files[0])
                        }
                    />

                    <InputError message={errors.profile_image_path} />

)

profile controler:

public function update(ProfileUpdateRequest $request, User $user): RedirectResponse
{
    $request->user()->fill($request->validated());

    if ($request->user()->isDirty('email')) {
        $request->user()->email_verified_at = null;
    }

    if($request->hasFile('profile_image_path')){
        if($user->profile_image_path){
            Storage::delete($user->profile_image_path);
        }

        $file = $request->file('profile_image_path');
        $name = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
        $ext = $file->getClientOriginalExtension();
        
        $user->profile_image_path = $request->file( 'profile_image_path' )->storeAs( 'public', $name . $ext );
    }

    $request->user()->save();
    return Redirect::route('profile.edit');
}

I have already entered php artisan storage:link

0 likes
3 replies
MohamedTammam's avatar

Replace this

 $file = $request->file('profile_image_path');
$name = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$ext = $file->getClientOriginalExtension();
        
$user->profile_image_path = $request->file( 'profile_image_path' )->storeAs( 'public', $name . $ext );

With the following

$user->profile_image_path = $request->file('profile_image_path')->store();

To get the displaying URL

Storage::url($user->profile_image_path);

https://laravel.com/docs/11.x/filesystem#file-uploads

el_maccho's avatar

@MohamedTammam still nothing has changed. I still have this type of path in the database: C:\Users\user\AppData\Local\Temp\phpA68D.tmp. Maybe it has to do with filesystem.php or env?

MohamedTammam's avatar

@el_maccho Maybe. What the following part gives you?

$request->file('profile_image_path')->store()

And how are you displaying it.

Please or to participate in this conversation.