henryoladj's avatar

Trying to Get the name of an uploaded Image

I am trying to get the name of an image and save it instead of saving it as laravel default hashing.

i.e if an image name is go.jpg it should save as go.jpg instead of randomly generated numbers

Here is my controller

private function storeImage($news)
    {
        if (request()->has('image')){
            $news->update([
                'image' => request()->image->store('uploads', 'public'),
            ]);

            $image = Image::make(public_path('storage/'. $news->image))->resize(600, 600);
            $image->save();
        }
    }
0 likes
24 replies
Sinnbeck's avatar

You can get the name like this

$original = $request->file('image')->getClientOriginalName();

$path = $request->file('image')->storeAs('uploads', $original);
henryoladj's avatar

@sinnbeck so it will be like this?

private function storeImage($news)
    {
        if (request()->has('image')){
            $news->update([
                'image' => request()->image->store('uploads', 'public'),
            ]);

            $original = $request->file('image')->getClientOriginalName();

            $path = $request->file('image')->storeAs($original);

            $image = Image::make(public_path('storage/'. $news->image))->resize(600, 600);
            $image->save();
        }
    }
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Close


private function storeImage($news)
    {
        if (request()->has('image')){
             $original = $request->file('image')->getClientOriginalName();

            $news->update([
                'image' =>  $request->file('image')->storeAs('uploads',$original);
            ]);

            

            $image = Image::make(public_path('storage/'. $news->image))->resize(600, 600);
            $image->save();
        }
    }
Sinnbeck's avatar

Oh sorry just replace $request with request()

I just personally prefer to use Dependency injection :)

henryoladj's avatar

@sinnbeck so after doing this

 private function storeImage($news)
    {
        if (request()->has('image')){
             $original = request()->file('image')->getClientOriginalName();

            $news->update([
                'image' =>  request()->file('image')->storeAs($original),
            ]);

            

            $image = Image::make(public_path('storage/'. $news->image))->resize(600, 600);
            $image->save();
        }
    }

I got this error Too few arguments to function Illuminate\Http\UploadedFile::storeAs(), 1 passed in C:\MAMP\htdocs\main\app\Http\Controllers\NewsController.php on line 198 and at least 2 expected

Sinnbeck's avatar

You are missing the directory


                'image' =>  $request->file('image')->storeAs('uploads', $original);
henryoladj's avatar

@sinnbeck well after doing that

private function storeImage($news)
    {
        if (request()->has('image')){
             $original = request()->file('image')->getClientOriginalName();

            $news->update([
                'image' =>  request()->file('image')->storeAs('uploads', $original),
            ]);

            

            $image = Image::make(public_path('storage/'. $news->image))->resize(600, 600);
            $image->save();
        }
    }

it is saying Image source not readable

ftiersch's avatar

@sinnbeck gave you an answer less than 10 minutes ago. Have you even TRIED solving the new error yourself and google it?

1 like
Sinnbeck's avatar

And it worked before these changes?

Try dd($news->image) after it is set

henryoladj's avatar

The upload is working though, it is just saying Image source not readable

ftiersch's avatar

Yeah. Or you google the errors and try to solve them.

Either you didn't upload a valid image or the image is not readable by PHP or you are passing the wrong path to Image::make()

1 like
Sinnbeck's avatar

If you locate the file on you computer does it work? In the /storage/uploads/ directory.

henryoladj's avatar

@sinnbeck i have solved it

private function storeImage($news)
    {
        if (request()->has('image')){
             $original = request()->file('image')->getClientOriginalName();

            $news->update([
                'image' =>  request()->file('image')->storeAs('uploads', $original),
            ]);

            $image = Image::make(public_path('image/'. $news->image))->resize(600, 600);
            $image->save();
        }
    }
Sinnbeck's avatar

The answer marked as best is just you changing your wrong path ? The answer to the actual question has been posted multiple times by me.

Sinnbeck's avatar

How does it not work? Do you get an error or? It might be a good idea to create a new topic as this one is listed as solved :)

Nakov's avatar

@sinnbeck it cannot be unsolved :) but as long as it is the same issue, it does not matter if it is solved or not I guess :)

Sinnbeck's avatar

@nakov My though was just that the issue was "How to get the name of an uploaded image", and problems working with files on a shared hosting server is quite another matter :) But I am happy to help either way

Nakov's avatar

@sinnbeck you are right.. I just read the last comment.. didn't put the title into thought. Sorry about interfering :)

Please or to participate in this conversation.