Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

david001's avatar

Delete file dropzone+laravel

How to delete particular file form dropzone and database This is my upload function:

 public function postUpload(Request $request,$id)
    {
         
        $destinationPath = 'images'; 
        $fileName=  $request->file('file')->getClientOriginalName(); 
        
        $upload_success = $request->file('file')->move($destinationPath, $fileName); 
     if($upload_success){
      
            $image = new Image;
            $image->filename = $fileName ;
           
            $image->save();

      }

 }

This is my view js :

<script>
Dropzone.options.myAwesomeDropzone = {
            maxFilesize: 2, // Size in MB
            addRemoveLinks: true,
            removedfile: function(file) { 

            var name = file.name;   
            console.log(file.name);       
            $.ajax({
              headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
         },
            type: 'POST',
            url: '/delete',
            data: {filename:name},
            dataType: 'html'
        });
            var fileRef;
                return (fileRef = file.previewElement) != null ? 
                      fileRef.parentNode.removeChild(file.previewElement) : void 0;
            },


                success: function(file, response) {
                  alert(response);
                 
                },


                error: function(file, response) {
                      alert(response);
                      return false;
                }
        };



</script>

This works perfectly . when i click remove link in dropzone after uploading file it deletes image from database ,But my problem is if two images are of same name in db then one click deletes both image,which i don't want

obviously i can do like this $fileName= $request->file('file')->getClientOriginalName(); $newFileName=$fileName.time();

so filename may be unique in db. but i need to delete file from database and through dropzone i get orginal file name

This is my delete method:

public function delete(Request $request){    
    $filename =  $request->get('filename');//gives orginal file name eg:abc.jpg
    Image::where('filename',$filename)->delete();
    return "ok";
   }
0 likes
2 replies
divanoli's avatar

Its kinda a long way. Along with the files uploaded send additional info file.lastModifiedDate.toLocaleDateString(),file.size and store it in db.

myDropzone.on("sending", function(file, xhr, formData) {
  // Will send the filesize along with the file as POST data.
  formData.append("filesize", file.size);
});

When you want to delete the file, accept the file in removedfile method and match the file size and modified date as well along with the file name. Delete the found match.

But still it may not be a 100% solution.

david001's avatar

yes it is not permanent solution

1 like

Please or to participate in this conversation.