How to delete a file from the server/system?

I want to delete the uploaded file when click on remove button from server permanently

Team Leader Asked on February 11, 2017 in PHP.
Add Comment
2 Answer(s)

    The unlink() function delete a file from server.

    
    <?php
    
    $file = "uploads/image.jpg";
    
    if (!unlink($file))
    
    {
    
    echo ("Error deleting $file");
    
    }
    
    else
    
    {
    
    echo ("Deleted $file");
    
    }
    
    ?>
    
    

    Team Leader Answered on March 19, 2017.
    Add Comment
      
      <div>
      
      <form name="deleteform" action="<?php echo $_SERVER['PHP_SELF'].'?delete=true';?> >
      
      <input type="button" name="picfile" value="<?php echo $file; ?>">
      
      <input type="submit" value="Delete">
      
      </div>
      
      

      PHP code:

      
      <?php
      
      if(isset($_GET['delete']) && $_GET['delete']=='true') {
      
      unlink("/uploads/".$_POST['picfile']);
      
      }
      
      ?>
      
      
      Team Leader Answered on March 19, 2017.
      Add Comment

      Your Answer

      By posting your answer, you agree to the privacy policy and terms of service.