Error = Method Illuminate\Http\UploadedFile::save does not exist - php

I can't add images to the database. I am always getting this error.
$this->validate($request, [
'imageName' => 'required',
'imagePath' => 'required',
]);
$image->productId = $request->input('productId');
$image->imageName = $request->input('imageName');
if (request()->hasFile('imagePath')){
$image = $request->file('imagePath');
$imageName = time() . '.' . $image->getClientOriginalExtension();
$destinationPath = public_path('/images/productImages/');
$image->move($destinationPath, $imageName);
$image->imagePath = $destinationPath . $imageName;
}
$image->imageCode = $request->input('imageCode');
$image->save(); // error line
return redirect()->route('image.index');
Thank you for your help

Actually from your code what I can understand is that you have an eloquent object and you want to upload an image and save the image data to eloquent object,
but inside your if (request()->hasFile('imagePath')){ } you have overwritten your $image variable :)
In that case change your code inside if conditional to the following
if (request()->hasFile('imagePath')){
$uploadedImage = $request->file('imagePath');
$imageName = time() . '.' . $image->getClientOriginalExtension();
$destinationPath = public_path('/images/productImages/');
$uploadedImage->move($destinationPath, $imageName);
$image->imagePath = $destinationPath . $imageName;
}

Related

How to save path after upload in laravel storage?

I want to after uploading an image save it like this
images/blogs/1650953308.jpg
But I see this
1650953308.jpg
I want to save like this
images/blogs/1650953308.jpg
public function store(Request $request)
{
$fileNameService = $request->file('image') ?? null;
if ($request->hasFile('image'))
{
$file = $request->file('image');
$name = time();
$extension = $file->getClientOriginalExtension();
$fileNameService = $name . '.' . $extension;
$img = Image::make($file->path());
$img->resize(600, 300, function ($constraint) {
$constraint->aspectRatio();
})->save('storage/images/blogs/'.$fileNameService);
}
Blog::query()->create([
'image' => $fileNameService,
]);
return redirect()->route('admin.blogs.index');
}
Concatenate folder path with image name.
Blog::query()->create([
'image' => 'images/blogs/'.$fileNameService,
]);
It will save as images/blogs/1650953308.jpg.

Upload file in Laravel - wrong path

I am beginner in Laravel.
I have this code:
if ($request->hasfile('profilePhoto')) {
$this->validate($request, [
'profilePhoto' => 'required',
'profilePhoto.*' => 'mimetypes:image/jpg'
]);
$image = $request->file('profilePhoto');
$extension = strtolower($image->getClientOriginalExtension());
$path = 'upload/images/UserImage/';
$uniqueName = md5($image . time());
$image->move(public_path($path), $uniqueName . '.' . $extension);
}
This function uploads files to public/upload/images/UserImage/.
I need it to store it in storage/app/upload/images/UserImage/ instead
How can I rewrite my code?
You have to use storage_path function ("storage/app/upload" folder must exist):
$image->move(storage_path("app/upload"), $uniqueName . '.' . $extension);
if ($request->hasfile('profilePhoto')) {
$this->validate($request, [
'profilePhoto' => 'required',
'profilePhoto.*' => 'mimetypes:image/jpg'
]);
$image = $request->file('profilePhoto');
$extension = strtolower($image->getClientOriginalExtension());
$path = storage_path('app/public/upload/images/UserImage');
$uniqueName = md5($image . time());
$image->move(public_path($path), $uniqueName . '.' . $extension);
}
A common way in Laravel to upload to storage/app is through the local disk driver.
$file = $path . $uniqueName . '.' . $extension;
\Storage::disk('local')->put($file, $request->file('profilePhoto'));
Storage::disk('local') points to storage/app/.
As your $path variable is already declared like this $path = 'upload/images/UserImage/' .So instead of storing data to public_path you can store to storage_path().
$image->move(storage_path($path), $uniqueName . '.' . $extension);

Image uploaded in Laravel 5.8 keep saving as private image

I am trying upload a single image into a Post and it keeps saving it as a private image--heck, I don't even know where to find that file.
PostController.php
...
if ($request->hasFile('photo')) {
$photo = $request->photo;
$ext = $photo->getClientOriginalExtension();
$filename = uniqid() . '.' . $ext;
$photo->storeAs('public/posts/' . $request->user()->id, $filename);
}
...
storeAs() function takes three parameters.
storeAs(path,name,options)
'options' is where you specify file visibility. In your case :
if ($request->hasFile('photo')) {
$photo = $request->photo;
$ext = $photo->getClientOriginalExtension();
$filename = uniqid() . '.' . $ext;
$photo->storeAs('posts/' . $request->user()->id, $filename,'public');
}

laravel image upload error

i'm using intervention package to upload images with laravel and all things are fine from creating to saving image but the problem in the image column in database it stores this name with path
/private/var/folders/18/0w78rt691m99y_kv8xln4n0c0000gn/T/phpFkP3Gh
this is my code:
if($request->hasFile('image')) {
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/' . $filename);
\Image::make($image)->save($location);
}
and this is the image name stored in images file 1503847676.jpg
please help me to find, where is the problem?
this is my whole method
public function update(Request $request, $id)
{
// Validating fields requests
$this->validate($request, [
'title' => 'required|min:6',
'subtitle' => 'required|min:6',
'slug' => 'required',
'body' => 'required',
'image' => 'required|mimes:jpeg,png,jpg,gif,svg'
]);
if($request->hasFile('image')) {
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/' . $filename);
\Image::make($image)->save($location);
}
//find target post
$post = Post::find($id);
//create upadeted data from inputs fields
$post->title = $request->title;
$post->subtitle = $request->subtitle;
$post->image = $filename;
$post->slug = $request->slug;
$post->image = $request->image;
$post->status = $request->status;
$post->body = $request->body;
//save the new data to database
$post->save();
$post->tags()->sync($request->tags);
$post->categories()->sync($request->categories);
return redirect('/admin/post');
}
You don't need Intervention package for simple image upload w/o any modifications like resize etc. Try this
if( $request->hasFile('image')) {
$image = $request->file('image');
$location = public_path(). '/images/';
$filename = time() . '.' . $image->getClientOriginalExtension();
$image->move($location, $filename);
}
//find target post
$post = Post::find($id);
$post->image = $filename;
//remaining code
$post->save();
You can try this if you use Intervention image
$image = Input::file('image');
$filename = time() . '.' . $image->getClientOriginalName();
$path = public_path('images/' . $filename);
Image::make($image->getRealPath())->save($path);
$post->image = 'images/' . $filename;
I had a similar error like yours, I solved it by using the following:
$file = Input::file('image');
$image->move($location, $file->getClientOriginalName());

move 'tmp' directory not working when uploading image in laravel 5.4

When uploading my image it is saved to D:\xampp\tmp\phpD0E0.tmp directory. But I want to save it in public/uploads/banner. Anyone please help me. Here is my code:
BannersController.php
public function store(Request $request)
{
$requestData = $request->all();
if ($request->hasFile('banner_image')) {
foreach($request['banner_image'] as $file){
$uploadPath = public_path('/uploads/banner');
$extension = $file->getClientOriginalExtension();
$fileName = rand(11111, 99999) . '.' . $extension;
$file->move($uploadPath, $fileName);
$requestData['banner_image'] = $fileName;
}
}
Banner::create($requestData);
Session::flash('flash_message', 'Banner added!');
return redirect('Banner/banners');
}
Can you please revert that path to 'root' => storage_path('app/public')
and just try with change line:
$extension = $file->getClientOriginalExtension();
$fileName = rand(11111, 99999) . '.' . $extension;
with this:
$fileName = rand(11111, 99999) . '.' . $file->getClientOriginalName();
Hope this helps you.

Categories