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

Uladzimir's avatar

How to correct name of upload's image in database?

Have a problem with saving name of poster in database. I want to save files with their own names, not with default. That's why done next construction in my FilmController:

			if ($request->hasFile('poster')) {
                $poster = $request->file('poster');
                $name = $poster->getClientOriginalName();
                $poster->storeAs('poster', $name);
            }

But in this case name of poster looks like "C:\xampp\tmp\phpBF23.tmp" in database. How to fix it and do, that file will save with it's original name. For example 4563s.jpg

For full description, the code in controller looks so:

/**
     * Store a newly created resource in storage.
     *
     * @param  StoreFilmRequest  $request
     * @return RedirectResponse
     */
    public function store(StoreFilmRequest $request): RedirectResponse
    {
       try {
            $film = new Film($request->validated());
            if ($request->hasFile('poster')) {
                $poster = $request->file('poster');
                $name = $poster->getClientOriginalName();
                $poster->storeAs('poster', $name);
            }

            $tags = $film['tags'];
            unset($film['tags']);

            $film->save();
            $film->tags()->sync($tags);
        } catch (\Exception $exception) {
            abort(404);
        }

        return redirect(route('admin.films'));
    }
0 likes
6 replies
Sinnbeck's avatar

This code isn't saving to the database. Show the code that does

Uladzimir's avatar

@Sinnbeck Changed the original question, added the rest of the code that saves the information as well.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Try adding (replace image with column name)

$film['image'] = $name;
1 like
Sinnbeck's avatar

@Uladzimir happy to help. One thing to remember is that if ever have two images with the same name, you are in trouble. "poster.jpg" for instance

1 like
Uladzimir's avatar

@Sinnbeck know about it and for this use "id" like a counstruction of file name, ids always different:)

Please or to participate in this conversation.