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

Krubbit's avatar

Uploading a file and getting new name to database

Hello, i love tha simple it is to upload a file in laravel 5.3, i saw the laracast and simply copied the code to upload a file.

request()->store('image')->store('posts');

That makes the magic of uploading the file to the storage but, how do i get back the unique name that laravel gave the image and make the controller update it to the database as that name ?

public function update($id, UpdatePostRequest $request)
    {
        request()->file('image')->store('posts');
        $post = $this->postRepository->findWithoutFail($id);

        if (empty($post)) {
            Flash::error('Post not found');

            return redirect(route('posts.index'));
        }

        $post = $this->postRepository->update($request->all(), $id);

        Flash::success('Post updated successfully.');

        return redirect(route('posts.index'));
    }
0 likes
3 replies
Szyfr's avatar

this?

$path = request()->file('image')->store('posts');

try, dd($path), you will see the unique name.
Krubbit's avatar

Ok that worked but how do i update the $request variable so i can update it in the database?

Allready tryed:

$fileName= request()->file('image')->store('posts');
        $request->merge([ 'image' => $fileName ]);

and

$FileName= request()->file('image')->store('posts');
        $request->replace(array('image' => $FileName));

even

$request->image = request()->file('image')->store('posts');

The update function it`s here:

$post = $this->postRepository->update($request->all(), $id);

and it always gives a name like:

C:/wamp/tmp/phpBB14.tmp
jlrdw's avatar

You need to lookup getClientOriginalName() in the api docs. example:

public function add() {
        //$this->chklog();
        // Session::set('thisadd','z');
        if (isset($_POST['submit'])) {
            echo "made it here";
            $lid = DB::table('dogs')->count();
            $lid = $lid + 1;
            $file = Input::file('ufile');
            $file_name = $file->getClientOriginalName();
            $file_ext = $file->getClientOriginalExtension();

            $fileInfo = pathinfo($file_name);
            $filename = $fileInfo['filename'];




            $newname = $filename . $lid . "." . $file_ext;
            $destinationPath = ROOTDIR . 'upload/imgdogs/';
            //Input::file('ufile')->move($destinationPath, $newname);
            $file->move($destinationPath, $newname);


            $dogpic = Cln::fixValue($newname);
            $dogname = ucfirst(Cln::fixValue($_POST['dogname']));
            $sex = ucfirst($_POST['sex']);
            $comments = Cln::fixValue($_POST['comments']);
            $adopted = (isset($_POST['adopted']) == '1' ? '1' : '0'); ///added
            $lastedit = date("Y-m-d H:i:s");
            echo "adpt=" . $adopted . "aaa";

            if (!isset($error)) {
                $postdata = array(
                    'dogpic' => $dogpic,
                    'dogname' => $dogname,
                    'sex' => $sex,
                    'comments' => $comments,
                    'adopted' => $adopted,
                    'lastedit' => $lastedit
                );

                DB::table('dogs')->insert($postdata);
            }
        }

        $this->layout = 'addtpl';
        return View::make('Dogs/Add')->shares('title', 'Dog Add');
    }

Here I want unique name so I am turning Rover.jpg into something like Rover257.jpg as an example.

Just a quick example using post, convert to laravel request etc etc

lions tigers and bears oh my

Of course @you_know_who will come up with even a better answer. It's not immick

Please or to participate in this conversation.