skip to Main Content

How can I delete an excel file form server with Javascript Apache I try this code but no works.

var r = confirm("Are you sure you want to delete this File?")
        if(r == true)
        {
       $.ajax({
       url:'delete.php',
       data:'https://example/server/index.xlsx',
       method:'GET',
       success:function(response){
        if (response === 'deleted')
        {
           alert('Deleted !!');
        }
       }
      });
        }
  

delete.php

<?php 
   unlink($_GET['file']);
?>

2

Answers


  1. Next time you should include the error message(s) you get so its easier for whoever is helping to understand your problem.

    Try this

    if(confirm("Are you sure you want to delete this file?")) {
    
        $.ajax({
            type: "POST",
            url: "delete.php", //assuming this file is in the same directory as the current file
            data: { filename: "/path/to/delete-file.xlsx" }, //this is the absolute path to your file you want deleted
            success: function(response) {
                if(response.success) {
                    alert(response.message);
                }
            },
            error: function(xhr, opt, thrownError) {
                //alert(xhr.responseText);
                //alert(thrownError);
            }
        });
        
    }
    

    This is your PHP delete.php file content

    <?php
        header('Content-type: application/json');
        
        $result = array(
            "success"   => 0,
            "message"   => "",
        );
        
        if(isset($_POST['filename'])) {
            if(file_exists($_POST['filename'])) {
                if(unlink($_POST['filename'])) {
                    $result = array(
                        "success"   => 1,
                        "message"   => "File deleted successfully!",
                    );
                }
            }
        }
        
        echo json_encode($result);
    ?>
    
    Login or Signup to reply.
  2. First of all, you can’t delete files with JS as it is run locally. You need to use server-side file to do that.

    delete.php

    <?php
    if(isset($_POST['path'])) {
        $path = $_POST['path'];
    
        //file exist?
        if(file_exist($path)) {
          //Remove file
          unlink($path);
    
         //Set status - deleted
         echo 'deleted';
      } else {
        echo 'not-deleted';
      } 
    die;
    }
    die;
    

    // Ajax file

        var r = confirm("Are you sure you want to delete this File?")
              if(r == true)
                {
    var file_path = 'https://example/server/index.xlsx';
               $.ajax({
               url:'delete.php',
               type:'POST',
               data:{path: file_path},
    
               success:function(response){
                if (response === 'deleted')
                {
                   alert('Deleted !!');
                }
               }
              });
                }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search