Hamed Azimi
Hamed Azimi

Reputation: 145

Delete a file using php

I use this code to show a list of files in user's profile page:

public static function getUserProfilemusic() {
    $path = Xenforo_Application::getInstance()->getRootDir() . '/styles/default/dadparvar/profilemusic/songs/';
    $directoryList = scanDir($path.'/'.func_get_arg(1));
    unset($directoryList[0]);
    unset($directoryList[1]);
    $string = '';
    foreach ($directoryList as &$listEntry) {
        $songURL = /*$path*/ '/styles/default/dadparvar/profilemusic/songs/' . func_get_arg(1) . '/'. $listEntry;
        $string .= "<a href='$songURL' class='Tooltip' title='Click to Download  $listEntry'> $listEntry </a>
                |  <a href='#' class='Tooltip' title='Click to Remove  $listEntry' target='_blank'> X </a>
                 <br>
               ";
    }
    return $string;
}

How can I set, when user clicked on X the file be deleted?

Any opinion will be appreciated.

Upvotes: 0

Views: 93

Answers (3)

Ren&#233; H&#246;hle
Ren&#233; H&#246;hle

Reputation: 27295

That depends a bit on your structure but the easiest way is to send the filename to a new script for example deletefile.php in that file you first check if you're logged in. Then you can check if the file exist and make an unlink on that file.

if(is_file($pathtofile."/".$filename)) {
    unlink($pathtofile."/".$filename);
}

Be patient that you check the input filename that you don't have an security hole in your application. To prevent some problems you should use the complete path to the file.

Upvotes: 2

Dut A.
Dut A.

Reputation: 1158

You need to do 2 things to achieve deletion of a file.

  1. Delete the file reference from a database (if stored).

  2. Delete the actual file from disk.

Sample functions for these actions:

    <?php
        public function deleteFromDb() {
          global $database;
          $sql = "DELETE FROM <$table_name> WHERE id = <ID> LIMIT 1";
          $database->query($sql);
          return ($database->affected_rows() == 1) ? true : false;
        }

        public function destroyFile() {
        // Remove the database entry
            if($this->deleteFromDb()) {
                // Remove the file
                $target_path = <PATH_TO_FILE_TO_DELETE>;
                return unlink($target_path) ? true : false;
            } else {
                // Failed to delete from db
                return false;
            }
        }
    ?>

Upvotes: 1

Jeff
Jeff

Reputation: 1004

You will need to define the path of the file you want to delete and preform a PHP function with unlink() to preform a PHP function onclick you can use AJAX

<a href='myAjax()' class='Tooltip' title='Click to Remove  $listEntry' target='_blank'>

function myAjax() {
      $.ajax({
           type: "POST",
           url: 'ajax.php',
           data:{action:'call_this'},
           success:function(html) {
             alert(html);
           }

      });
 }

ajax.php

    if($_POST['action'] == 'call_this') {
     $listEntry = 'file_path' 
     unlink($listEntry);
    }

Upvotes: 1

Related Questions