0

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.

scrowler
  • 24,273
  • 9
  • 60
  • 92
Hamed Azimi
  • 145
  • 10

3 Answers3

2

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.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • 2
    For security you may also want to check to make sure the file being deleted was uploaded by the user deleteing it so users cant delete other users files – Jeff Apr 17 '16 at 21:03
1

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);
    }
Community
  • 1
  • 1
Jeff
  • 1,018
  • 1
  • 15
  • 33
1

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;
            }
        }
    ?>
Dut A.
  • 1,029
  • 11
  • 22