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();
}
}
You can get the name like this
$original = $request->file('image')->getClientOriginalName();
$path = $request->file('image')->storeAs('uploads', $original);
@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();
}
}
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 i got this error Undefined variable: request
Oh sorry just replace $request with request()
I just personally prefer to use Dependency injection :)
@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
You are missing the directory
'image' => $request->file('image')->storeAs('uploads', $original);
@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
@sinnbeck gave you an answer less than 10 minutes ago. Have you even TRIED solving the new error yourself and google it?
And it worked before these changes?
Try dd($news->image) after it is set
The upload is working though, it is just saying Image source not readable
@ftiersch yes i have and i am pasting the errors i am getting here
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()
@sinnbeck after doing dd($news->image); i could see this uploads/Freaky.jpeg
If you locate the file on you computer does it work? In the /storage/uploads/ directory.
@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();
}
}
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 the code does not seem to work on live shared hosting server
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 :)
@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 :)
@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
@sinnbeck you are right.. I just read the last comment.. didn't put the title into thought. Sorry about interfering :)
@nakov No worries :) Always happy to get input
Please sign in or create an account to participate in this conversation.