1

I save multiple images using array in database. eg. img.jpg,img1.jpg,img2.jpg this is my code.

$this->validate($request,[
        'listing_id' => 'required',
        'images.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        'description' => 'required',
        'features' => 'required',
        'make' => 'required',
    ]);

    $vehicles = vehicle::find($id);
    $input=$request->all();
    $images=array();
    if($files=$request->File('images')){
        foreach($files as $file){
            $name=date('mdYHis').uniqid().'.'.$file->getClientOriginalExtension();
            $file->move('uploaded-images',$name);
            $images[]=$name;
            $vehicles->images = implode(",",$images);
        }
    }else{

        $vehicles->images = $request->images_dummy;

    }

I want is to delete the image file in public/uploaded-images. i found this How to Delete Images from Public/Images Folder in laravel 5 (URL Data) but its not working in my code.

2
  • So you store a comma separated list of images, and when updating you want to delete all of the images in the comma separated list - is that right? Commented Nov 13, 2018 at 5:38
  • yes sir .. i use this when uploading the images vehicles->images = implode(",",$images); Commented Nov 13, 2018 at 5:42

2 Answers 2

2

As you store the images as a comma separated list, you just need to explode the string (which produces an array) and then loop over it to delete the images.

Something like this should work:

use Illuminate\Support\Facades\Storage;

$images = explode(",", $vehicles->images);

foreach ($image as $images) {
    Storage::delete("uploaded-images/{$image}");
}

If you changed your code to store the image names as the actual path to the image (i.e uploaded-images/img.jpg,uploaded-images/img1.jpg then you could alter this to be a but simpler:

use Illuminate\Support\Facades\Storage;

$images = explode(",", $vehicles->images);
Storage::delete($iamges);

Both of these examples can be found in the Laravel docs.

Sign up to request clarification or add additional context in comments.

4 Comments

$img = explode(",", $vehicles->images); Storage::delete($img); if($files=$request->File('images')){ foreach($files as $file){ $name=('uploaded-images/').date('mdYHis').uniqid().'.'.$file->getClientOriginalExtension(); $file->move('uploaded-images',$name); $images[]=$name; $vehicles->images = implode(",",$images); } }else{ $imagess = $request->images_dummy; $vehicles->images = $imagess; }
i updated my code but still adding images in uploaded-images
Yes, your code still adds images to the uploaded-images folder. First you are deleting the existing images, then you are adding the new ones. Is this not what you wanted?
yes sir already solved .. i used use use File; instead of storage foreach(explode(',', $vehicles->images) as $img) { File::delete('uploaded-images/'.$img); }
1

You should try this:

use File;

$images = explode(",", $vehicles->images);

foreach($images as $image){

   $image_path = public_path().'/images/'.$image;

   if(File::exists($image_path)) {
    File::delete($image_path);
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.