我使用此代码在用户的配置文件页面中显示文件列表:
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;
}
当用户单击X时,如何设置文件被删除?
任何意见都将不胜感激。
这在一定程度上取决于您的结构,但最简单的方法是将文件名发送到一个新脚本,例如您首先检查是否登录的文件中的deletefile.php
。然后您可以检查该文件是否存在,并在该文件上生成unlink
。
if(is_file($pathtofile."/".$filename)) {
unlink($pathtofile."/".$filename);
}
请耐心检查输入文件名,确保应用程序中没有安全漏洞。为了防止出现某些问题,您应该使用文件的完整路径。
您需要定义要删除的文件的路径,并使用unlink()预生成一个PHP函数。单击可以使用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);
}
您需要做两件事才能实现文件的删除。
-
从数据库中删除文件引用(如果已存储)。
-
从磁盘中删除实际文件。
这些操作的示例函数:
<?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;
}
}
?>