1

I am using laravel 5.4 in my project i displayed my images inside source file in laravel blade it shows the image correctly..

 <div  id="dis_img">
                    @foreach($detail_dir_imgs as $detail_dir_img)
                      <img src="{{ asset($detail_dir_img) }}" height="230" width="200" id="pro_img">
                      <span class="del_proimg">X</span>
                    @endforeach
                  </div> 

Now i get the image src attribute inside the del_proimg click function which means i want to delete the image when i click this id and the code inside my click function is

$('.del_proimg').click(function(){
      var img= $('#pro_img').attr("src");
      alert(img);
       $.ajax({
        type: "GET",
        url: '/deleteimg',
        data: {'img':img},
        success: function(data){
          console.log("ajaxdata",data);
        },
        error: function(jqXHR, textStatus, errorThrown) {
          console.log(JSON.stringify(jqXHR));
          console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
        }
      });
    });

When the id is clicked it get the imagepath and pass the data to controller everything worked fine but in my controller when i try to delete the image path it doesnot delete the image and my controller code is

  public function deleteimg(Request $request){
            $img =$request->img;
            @unlink(public_path( $img));
            return Response::json([
              'message' => $request->img
          ], 200);

        }

Is this is correct i tried this also File::delete($img) but its not delete my image in my folder but no error shows please anyone help me to fix this isssue

1
  • Maybe file-access rights? Commented May 17, 2017 at 13:21

3 Answers 3

4

You are passing entire image source to function deleteimg as $img. Just sent image name .Then use unlink(PATH TO YOUR IMAGE "/" IMAGE NAME). Like you may save the image in puplic/uploads then use unlink(public_path().'/uploads/'.$imageName);

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

8 Comments

yeah thank you but how i get the image name alone in jquery
You can use the currrent $img as it is .Just explode the $img by "/" and get the last array element.
can you give me the code i doknow how to use explode function
$img = "aaa/bb/a.jpg"; $imgArray = explode('/',$img); $imageName = $imgArray[count($imgArray)-1]; echo $imageName;
by using explode('/',$img) it split the array now how i get the last element
|
0

try this

unlink(public_path().'/'.$img);

this work fine for me

Comments

0

This might work. Just send image path through ajax call. This will return true if file is deleted. And check your route as well in web.php file

use Illuminate\Support\Facades\Storage;

public function deleteimg($file_path){
      Storage::delete($file_path);
      return response()->json(array('success' => true));
}

2 Comments

it working I found whats the problem but i doknow how to get the solution when i return my $img variable it shows like this Object {message: "http://birdys.app/uploads/products/images/50/11474…M-Unisex-Table-Covers-9731474522719404-1_mini.jpg"} in my console window but when i try to remove that "birdys.app" it working correctly
Any other problem than file delete ?

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.