In this tutorial, We will learn how to remove uploaded files. It's a simple example of dropzone delete the uploaded file.


Create some files & folder:

  • index.php
  • upload.php
  • upload folder


index.php

<!DOCTYPE html>
<html>
<head>
  <title>How to Delete Uploaded File in Dropzone JS?</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <link href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.0.1/min/dropzone.min.css" rel="stylesheet">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.2.0/min/dropzone.min.js"></script>
</head>
<body>
  
<div class="container">
  <div class="row">
    <div class="col-md-12">
      <h2>How to Delete Uploaded File in Dropzone JS</h2>
      <form action="upload.php" enctype="multipart/form-data" class="dropzone" id="image-upload">
        <input type="hidden" name="request" value="add">
        <div>
          <h3>Upload Multiple Image By Click On Box</h3>
        </div>
      </form>
    </div>
  </div>
</div>
  
<script type="text/javascript">
  
    Dropzone.autoDiscover = false;
  
    var myDropzone = new Dropzone(".dropzone", { 
       maxFilesize: 10,
       acceptedFiles: ".jpeg,.jpg,.png,.gif",
       addRemoveLinks: true,
       removedfile: function(file) {
         var fileName = file.name; 
           
         $.ajax({
           type: 'POST',
           url: 'upload.php',
           data: {name: fileName,request: 'delete'},
           sucess: function(data){
              console.log('success: ' + data);
           }
         });
  
         var _ref;
          return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
       }
    });
      
</script>
  
</body>
</html>



upload.php

<?php
  
$uploadDir = 'upload';
  
if ($_POST['request'] == "add") {
    if (!empty($_FILES)) {
     $tmpFile = $_FILES['file']['tmp_name'];
     $filename = $uploadDir.'/'.$_FILES['file']['name'];
     move_uploaded_file($tmpFile,$filename);
    }
}else{
    $fileName = $uploadDir.'/'.$_POST['name'];  
    unlink($fileName); 
}
  
?>


Finally, Create upload folder and run your application.


Thanks....