Laravel can't upload file in storage folder - php

I used to store my upload files (images) in my public folder but this time I want to store them in my storage folder and I get this error
Can't write image data to path (C:\laragon\www\mynewsite\storage\images/aboutimage-1522481830.png)
Error comes from this line (where I use intervention/image package)
Image::make($image)->resize(1200, 600)->save($location);
My function:
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('images/' . $filename);
Image::make($image)->resize(1200, 600)->save($location);
$oldFilename = $indexabout->image;
$indexabout->image = $filename;
Storage::delete($oldFilename);
}
any idea?
UPDATE
My files will upload in root/storage folder instead of root/storage/app/public/images
why is that?
Update 2
I changed my function to code below and it's uploading where it suppose to upload but it does not delete the old image
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = '/aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('app/public/images' . $filename);
Image::make($image)->resize(1200, 600)->save($location);
$oldFilename = $indexabout->image;
$indexabout->image = $filename;
Storage::delete($oldFilename);
}

SOLVED
here is final code:
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('app/public/images/' . $filename); // root storage path
Image::make($image)->resize(1200, 600)->save($location);
Storage::delete('images/' . $indexabout->image); //public storage path
$indexabout->image = $filename;
}
Basically I gave Root/Storage path to store data and Public/Storage path for deleting them in update method.

Related

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');
}

Moving file uploads to specific folder with Laravel

I am having issues getting my image uploads to move to the proper folders. When I upload them they just dump into the main img folder instead of going to the specific subfolder.
Here is the code I have:
$rental = new Rental;
$rental->title = $request->title;
$rental->name = $request->name;
$rental->description = $request->description;
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('img/rentals' . $filename);
Image::make($image)->save($path);
$rental->image = $filename;
}
$rental->save();
You're missing a / in your $path. The following line...
$filename = time() . '.' . $image->getClientOriginalExtension();
will generate a string, something like 123456789.jpg. Then in the next line you're doing...
$path = public_path('img/rentals' . $filename);
which joins together img/rentals and 123456789.jpg, resulting in img/rentals123456789.jpg. Notice that . which concatenates two strings. You're then passing the resulting string to public_path which will refer to a folder in your public directory named img and a filename rentals123456789.jpg.
To solve your problem you just need to stick a forward slash in between:
$filename = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('img/rentals/' . $filename);
which will result in a folder img/rentals inside your public path and a filename of 123456789.jpg.

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.

Delete image after upload new one

I'm working with laravel 5.4 I have a form which I can upload my logoimage and in update function I have this code:
//Save logo
if ($request->hasFile('logo')) {
$avatar = $request->file('logo');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
$location = public_path('avatars/logos/');
$request->file('logo')->move($location, $filename);
$oldFilename = $general_Settings->logo;
$general_Settings->logo = $filename;
Storage::delete($oldFilename);
}
$general_Settings->save();
for updating my image which is work but as you see I have Storage::delete($oldFilename); this part doesn't work and just keep the old image.
what do you think is issue of that?
Solved:
The issue was Filesystem.php I made my local root set to 'root' => public_path('avatars/'), and changed all my functions in my app because no way to save images in sub-folders and delete them just can save in sub-folders.
then my update function become like this:
if ($request->hasFile('logo')) {
$avatar = $request->file('logo');
$filename = 'sitelogo' . '-' . time() . '.' . $avatar->getClientOriginalExtension();
$location = public_path('avatars/');
$request->file('logo')->move($location, $filename);
$general_Settings->logo = $filename;
}
$general_Settings->save();
I hope this help someone.
Since, You have stored logo file name only not full path of file,
You need to give full path from public folder to delete image. Change delete line as:
if ($request->hasFile('logo')) {
$avatar = $request->file('logo');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
$location = public_path('avatars/logos/');
$request->file('logo')->move($location, $filename);
$oldFilename = $general_Settings->logo;
$file_path = "avatars/logos/".$oldFilename;
$general_Settings->logo = $filename;
Storage::delete($file_path);
}
$general_Settings->save();
This might work.

Saving a remote image from facebook graph api

Any idea what's wrong?
I end up with 1bytes files and they are wrong. Using Intervention Image & PHP. Tried many ways to get it but nothing... If I just want to display it works but I can't save the picture...
$avatar_url = 'http://graph.facebook.com/' . $id . '/picture?type=square&width=140&height=140&redirect=false';
$avatar_pic_url = json_decode(file_get_contents($avatar_url), true)['data']['url'];
dd($avatar_pic_url);
$avatar = Image::make($avatar_pic_url);
$extension = 'jpg';
// save it
$destinationPath = 'uploads/avatars/';
$filename = uniqid(). '.' . $extension;
$path_to_temp_image = $avatar->dirname . '/' . $avatar->filename;
$key = $destinationPath . $filename;
// Upload avatar to remote storage
$uploadSuccess = Storage::put($key, $path_to_temp_image);

Categories