I have a small problem when I try to upload an image from POSTMAN to my LARAVEL API, I am creating the path and name well, but when making the POST request the image is not saved in the supposed folder where it should be saved.
The route is for the creation of a product, and the image corresponds to it:
Route:
Route::post('product/new','ProductController#store');
In my controller:
$product = new Product;
$product->name = $request->input('name');
//Other staff
$file = $request->file('image');
$extension = $file->getClientOriginalExtension();
$filename = $product->name . '.' . $extension;
$path = $file->move(public_path("/uploads/image/products/"), $filename);
$product->image = $path;
And the POSTMAN config:
Already try to configure the Content type headers with: x-www-form-urlencoded, with Content type multipart/form-data and without any Content type, and none of the 3 worked
The result I'm getting is:
{
"id": 1,
"name": "test1",
//staff
"image": "/app/public/uploads/image/products/test1.png",
}
But if I check there:/app/public/uploads/image/products/ there are no saved images
Thank you in advance :)
Use the store method on the file from the request instead and symlink the storage directory for the public
$extension = $file->getClientOriginalExtension();
$filename = $product->name . '.' . $extension;
$request->image->storeAs('uploads/image/products/', $filename);
And run php artisan storage:link
Related
I'm trying to integrate Intervention/Image into my laravel project to create a thumbnail upon uploading an image.
The image upload itself works fine, it doesn't seem to have any issue recognizing Intervention itself.
Below is the code block. The error seems to happen on the line with the save statement, I'm able to die and dump the contents of $img after it's set.
$file = $request->file('image');
$name = md5($file->getClientOriginalName() . time());
$extension = $file->getClientOriginalExtension();
$fileName = $name . '.' . $extension;
$file->move('./uploads/images/', $fileName);
$img = Image::make($file)->fit(300);
$img->save('/uploads/thumbnails/' . $name, 60, 'jpg');
This is the error I'm getting:
SplFileInfo::getSize(): stat failed for /private/var/folders/87/p5x7mgy914qg9ytf2zccc6q00000gn/T/php3lshFS
After some searching I've found that this could be related to file size upload limits, but I've altered my php.ini file (all of this is local btw) to accept 20MB files and the file I'm trying to upload is less than 100kb. I've also reset both php through homebrew and apache. Still getting the error.
Is there any glaringly obvious issues in my use of Intervention? I'll happily provide more info, this is in the store function in one of my controllers btw.
Untested, but I do it like this:
public function thumbnail(Request $request){
$thumbDir= storage_path('app/public').'/uploads/thumbnails/';
$file = $request->file('image');
$filename = md5($file->getClientOriginalName() . time()).'.jpg';
// $name = md5($file->getClientOriginalName() . time());
// $extension = $file->getClientOriginalExtension();
// $fileName = $name . '.' . $extension;
// $file->move('./uploads/images/', $fileName);
Image::make($file)->encode('jpg', 60)->fit(300, null, function ($c) {
$c->aspectRatio();
$c->upsize();
})->save($thumbDir . $filename);
return back()->with('success','The Image Has Been Added.');
}
I use code below:
$image_name = 'image' . time() . '.' . $request->file('image')->getClientOriginalExtension();
$destinationFolder = public_path('images');
$request->file('image')->move($destinationFolder, $image_name);
but sometime its not working, images are not storing. Im using heroku as host.
Try to use Storage facade :
$path = \Storage::putFile('images', $request->file('image'));
Laravel will generate a name automatically, About Storage facade, just see this. :)
I made function for upload file and if there is no uploaded file do it again till its upload
$imageFile->move($destinationFolder, $fileName);
if(file_exists(public_path('images') . '/' . $fileName))
return public_path('images') . '/' . $fileName;
else
self::uploadImage($imageFile, $fileName);
I used the code and it's working fine.
self::$image = $request->file('image');
self::$imageName = self::$image->getClientOriginalName();
self::$directory = 'category-images/';
self::$image->move(self::$directory, self::$imageName);
return self::$directory.self::$imageName;
I have difficulty understanding the file save and retrieval in Laravel. I managed to get the file saved into correct path
$fileNameWithExt = $request->file('Agreement_file')->getClientOriginalName();
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
$extention =$request->file('Agreement_file')->getClientOriginalExtension();
$filenameToStore = $fileName . '_' . $lab_id. '.'.$extention;
$request->Agreement_file->storeAs('agreements', $filenameToStore );
However not I want to create an a-tag to download the file, but cannot manage to get to download the file.
{{$filenameToStore}}
The file download but I get the error "Failed- Server Problem". I do not want to use a same link as these files are confidential and should not be able to be downloaded outside the app.
Something like that?
Step 1:
Create table files id | filename | user_id.
Step 2:
Create model File
Step 3:
Add file row in table.
$fileNameWithExt = $request->file('Agreement_file')->getClientOriginalName();
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
$extention =$request->file('Agreement_file')->getClientOriginalExtension();
$filenameToStore = $fileName . '_' . $lab_id. '.'.$extention;
$request->Agreement_file->storeAs('agreements', $filenameToStore );
File::create([
'filename' => $filenameToStore,
'user_id' => Auth::id()
]);
Step 4:
Create controller method download.
public function download(){
$filename = $request->input('filename');
$file = File::where('user_id', Auth::id())
->where('filename', $filename)
->firstOrFail();
$path = Storage::path('agreements/' . $filename);
if(Storage::exists($path)){
return Response::download($path, $filename);
}
}
Step 5:
Replace your link:
{{$filenameToStore}}
Or you can build it based on the file ID.
I have successfully created a Laravel 5.4 web application which is hosted on Heroku. I have also have managed that images gets uploaded on AWS S3. However the images gets uploaded as blank or empty. I have tried various online sources and have not been able to find a solution to my problem. Below I have attached my controller, which I believe is causing the error.
public function updateavatar(Request $request, User $user){
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
// $destinationPath = public_path('/uploads/avatars/');
$imageS3 = Image::make($avatar)->resize(300,300);
Storage::disk("s3")->put($filename, $imageS3->__toString());
//$disk->put("img/album/$id/$filename", $image->__toString());
$motcall = $this->carCheck($user);
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
An example how the issue looks like in my bucket
I have managed to resolve the issue by changign the storage line to Storage::disk("s3")->put($filename, file_get_contents($avatar)); P.S it ignores the image crop
encode worked for me:
for "s3"
Storage::disk('s3')->put('/downloads/' . $fileName, $interventionImage->encode(), 'public');
for local storage
Storage::disk('public')->put('/downloads/' . $fileName, $interventionImage->encode(), 'public');
I am trying to update images in my database.I am using laravel 5.4 version.
I am getting this error.
Type error: Argument 1 passed to Illuminate\Database\Eloquent\Builder::make() must be of the type array, object given, called in C:\xampp\htdocs\laravel_eshopper\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php on line 1357
Here is my update function in the controller script.
if use http://image.intervention.io/
Change Product::make($image) to Image::make($image)
Ah okay I know the problem.
First, the best practice is not save the image in database just save the path where you save the image.
Here I give you example code:
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/home/');
// now saving the file
$image->move($location, $filename);
// delete oldfile
$oldFilaname = $product->image;
Storage::delete($oldFilename);
// update
$product->image = $location . '/' . $filename;
}