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.

1
  • 1
    You'll need to post the filename to a script which you will delete files with Commented Apr 17, 2016 at 20:46

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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
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); } 

Comments

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; } } ?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.