How to delete image from storage in restfull api - laravel

$employee = Employee::find($id);
Storage::delete($employee->photo_file_path . $employee->photo_file_type);
$file = $request->image;
$path = $file->storeAs('public/images', $id . '.' . $file->getClientOriginalExtension());
$employee->photo_file_name = $id;
$employee->photo_file_type = $file->getClientOriginalExtension();
$employee->photo_file_path = $path;
when uploading photos, I want the previous photo to be deleted first and then insert a new photo again, I use Storage::delete. I have checked the public folder but the previous photo was not deleted, instead the photo was added

If you are uploading images in public folder then you need to add public before your path
Storage::delete('public/'.$employee->photo_file_path . $employee->photo_file_type);

if(file_exists(public_path('upload/bio.png'))){unlink(public_path('upload/bio.png'));
}else{dd('File does not exists.');}

Related

How to Save File in storage folder with Original name?

I want to store an uploaded file with its original client name in the storage folder. What do I need to add or change in my code?Any help or recommendation will be greatly appreciated
Here my Controller
public function store(Request $request) {
$path = "dev/table/".$input['id']."";
$originalName = $request->file->getClientOriginalName();
$file = $request->file;
Storage::disk('local')->put($path . '/' . $originalName, $request->file);
}
Edit: I know how to get the originalClientName. the problem is storing the file in the folder using the original name, not the hash name. It doesn't store in the file in the original it makes a new folder instead here is the output "dev/table/101/Capture1.PNG/xtZ9iFoJMoLrLaPDDPvc4DMJEXkRL3R4qWOionMC.png" what I trying to get is "dev/table/101/Capture1.PNG"
I have tried to use StoreAs Or putFileAs but the method is undefined
I managed to figure out how to store it with a custom name, for those who want to know how to do it here is the code
$id = $input['id'];
$originalName = $request->file->getClientOriginalName();
$path = "dev/table/$id/".$originalName;
Storage::disk('local')->put($path, file_get_contents($request->file));
public function store(Request $request) {
$originalName = $request->file->getClientOriginalName();
$extension = $request->file->getClientOriginalExtension();
$path = "dev/table/" . $input['id'] . "/" . $originalName . "." . $extension;
$file = $request->file;
Storage::disk('local')->put($path, $file);
}
To get the original file name you can use this in your ControllerClass:
$file = $request->file->getClientOriginalName();
To get additional the extension you can use this Laravel Request Method:
$ext = $request->file->getClientOriginalExtension();
Then you can save with:
$fileName = $file.'.'.$ext;
$request->file->storeAs($path, $fileName);
// or
Storage::disk('local')->put($path . '/' . $fileName , $request->file);
You can save files using storage with the default name using putFileAs function instead of put which allow take third param as a file name
$path = "dev/table/101/";
$originalName = request()->file->getClientOriginalName();
$image = request()->file;
Storage::disk('local')->putFileAs($path, $image, $originalName);
Update
You can do something like this with put,
Storage::disk('local')->put($path.$originalName, file_get_contents($image));
I tried to manage like this;
$insurance = $request->file('insurance_papers');
$insuranceExtention = $insurance->getClientOriginalExtension();
$path = "public/files/" . $carrier->id . "/insurance_papers." . $insuranceExtention;
Storage::disk('local')->put($path, file_get_contents($insurance));
You can try this, this is work for me
if ($request->hasFile('attachment')) {
$image = $request->file('attachment');
$imageName = time() . '.' . $image->getClientOriginalExtension();
$path = "foldername/".$imageName;
Storage::disk('public')->put($path, file_get_contents($image));
}

Storing image in local drive and saving path in db

I would like to save an image in my local drive, and insert its path to a DB, and then preview the image through the website. I have no idea how to do it and also I have not found anything similar anywhere.
Try this one
$image = $request->file('image');
$filename = time() . $image->getClientOriginalName();
$destination_path = public_path('/image/');
$image->save($destination_path . $filename);
$image->image_path= $destination_path;
Hope this helps :)
You can try to do this:
// To save image on local drive and insert path to db.
if ($request->hasFile('image') && $request->file('image')->isValid()) {
$path = $request->image->store('public/images');
$path = basename($path);
$image = new Images();
$image->photo = $path;
$image->save();
}
I hope it would be helpful.

Incorrect URL from AWS S3 with Laravel File Upload

I'm uploading an image to S3 with Laravel as follows:
$image = $request->image;
if (!empty($image)) {
$imageFileName = $user_id.'_'.rand(11111111, 99999999) . '.' . $image->getClientOriginalExtension(); // Rename Image
try
{
//Send to S3
$s3 = Storage::disk('s3');
$filePath = '/profile/' . $imageFileName;
$s3->put($filePath, file_get_contents($image), 'public');
$image = Storage::cloud()->url($imageFileName);
}
catch(\Exception $exx)
{
//Send to Logs Etc
}
The image uploads successfully but I need to store the URL in my database. This is being called here:
$image = Storage::cloud()->url($imageFileName);
The issue is the URL being returned, it looks like this:
http://test-env.XXX.us-west-2.elasticbeanstalk.com/profile/https://elasticbeanstalk-us-west-2-123XXX456XXX.s3.us-west-2.amazonaws.com/6_52644340.jpg
Hence:
http://mentr-test-env.2w8sh3esch.us-west-2.elasticbeanstalk.com/profile/
is somewhat-correct. But the next piece is missing the 'profile' sub-folder, and obviously starts at HTTPS again:
https://elasticbeanstalk-us-west-2-123XXX456XXX.s3.us-west-2.amazonaws.com/6_52644340.jpg
It would appear I'm getting two halves of the link in a single string. I don't edit the $image variable anywhere else.
The correct link is:
https://elasticbeanstalk-us-west-2-123XXX456XXX.s3.us-west-2.amazonaws.com/profile/6_52644340.jpg
I have confirmed the files are uploading correctly and publicly available.
I have tried calling:
$image = Storage::cloud()->url($filePath);
And this returns:
http://test-env.XXXX.us-west-2.elasticbeanstalk.com/profile/https://elasticbeanstalk-us-west-2-XXX123XXX.s3.us-west-2.amazonaws.com//profile/6_31595766.jpg
Update
I just noticed the first part of the returned URL is the BeanStalk instance URL with the /profile/ added. This is even stranger as I don't wish to use Beanstalk, I only want to use S3.
If you want to store the entire url you can get it from the return variable passed back from the put() function.
$s3_url = $s3->put($filePath, file_get_contents($image), 'public');
Sometimes I like to just store the path though and save just that piece to the database then I can pass the path to Storage::url($filePath); and it still works.
$image = $request->image;
if (!empty($image)) {
$imageFileName = $user_id.'_'.rand(11111111, 99999999) . '.' . $image->getClientOriginalExtension(); // Rename Image
try
{
//Send to S3
$s3 = Storage::disk('s3');
$filePath = '/profile/' . $imageFileName;
$s3->put($filePath, file_get_contents($image), 'public');
$image = Storage::cloud()->url('s3-url/s3-bucket'.'/'.$filepath);
}
catch(\Exception $exx)
{
//Send to Logs Etc
}

Laravel resize and upload to storage

I am trying to resize, rename and upload the image using laravel Storage, intervention on laravel5.
Here is my upload code:
if( $request->file('logo_image') ){
$logo_path = $request->file('logo_image')->store('university/'.Auth::User()->id);
if ( $logo_path ){
Storage::delete($university->logo_image);
}
$university->logo_image = $logo_path;
}
$university->update();
It is storing the image on folder like: storage/app/public/university/1/fjkdhjkfdh.jpg
Now I want to rename image name like university-name.jpg and also resize and upload to sam directory.
So I should have two images one is storage/app/public/university/1/university-name.jpg.jpg
and other is storage/app/public/university/1/thumbnail/university-name.jpg
I play around with this for the whole day but no success.
if(Input::file())
{
$image = Input::file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('profilepics/' . $filename);
Image::make($image->getRealPath())->resize(200, 200)->save($path);
$user->image = $filename;
$user->save();
}
Please someone can help me?
Thanks in advance.

Need to display catalog image not found rather than placeholder in magento

I have been stuck in a problem. I am using another server for image uploading. Let me elaborate on the issue first.
When I delete an image manually from the media folder, now placeholder image is getting showed up.
Initially
catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/c/o/controlbar-white-small.jpg
After Deleting image manually
catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/images/catalog/product/placeholder/image.jpg
My requirement is my store should still fetch the same image Url as was before deletion as image path in the database is still there.
Please help.
This is a core magento behavior so that product without image will show the placeholder instead of a broken image, so unless you make a lot of changes to core image/helper in app/code/core/Mage/Catalog/Model/Product/Image.php the only other way is to change placeholder.
Have you think about just change the placeholder image?
In admin, under System > Configuration > Catalog > Product Image Placeholders, you can upload new file.
core_config_data table
I have resolve my issue . I am mentioning the solution.
go to OnePica_ImageCDN-1.0.9\OnePica\ImageCdn\Model\Adapter\Amazons3.php
Replace this
public function getUrl($filename)
{
$type = Mage::app()->getStore()->isCurrentlySecure() ? 'url_base_secure' : 'url_base';
$base_url = Mage::getStoreConfig('imagecdn/amazons3/' . $type);
$filename = $base_url . $this->getRelative($filename);
return str_replace('\\', '/', $filename);
}
by
public function getUrl($filename)
{
$type = Mage::app()->getStore()->isCurrentlySecure() ? 'url_base_secure' : 'url_base';
$base_url = Mage::getStoreConfig('imagecdn/amazons3/' . $type);
$product = Mage::registry('current_product');
$productId = $product->getId();
$resource = Mage::getSingleton('core/resource')->getConnection('core_write');
$imageName = $resource->fetchOne("select value from catalog_product_entity_varchar where attribute_id=86 && entity_id = ".$productId);
$filename = $base_url . $this->getRelative($filename);
$filename = substr($filename, 0, -22);
$filename = $filename.$imageName;
return str_replace('\\', '/', $filename);
}

Resources