首先,您要创建一个php文件并将其命名为delete.php,并将下面的代码放置在其中。
<?php
//The path to your articles folder
$dirPath = "the_world_news_journal/finance/";
$dir = new DirectoryIterator($dirPath);
?>
<table style="width:70%">
<tr>
<td><b>Title</b></td>
<td><b>Date Posted</b></td>
<td><b>Delete Post</b></td>
</tr>
<?php
foreach ($dir as $fileinfo) {//begin foreach loop
//if the fileinfo is not equal to . or ..
if($fileinfo !== "." || $fileinfo !== ".."){//begin if then
//set the file path retrieving the file name of your articles
$filePath = "the_world_news_journal/finance/". $fileinfo->getFilename();
$filename = $fileinfo->getFilename();
$creationdate = date ("F d Y H:i", filectime($filePath));
//get the file path, name, creation date, and then and display it in the href inside
//a td tag
echo "<tr>
<td><a href ='$filePath'>$filename</a></td>
<td>$creationdate</td>
<td><a class='delete' href='$filePath'>Delete!</a></td>
</tr>
";
}//end if then
}//end foreach loop
?>
<!--import JQuery to delete a file-->
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script>
//fire event when the .delete class is clicked on
$(".delete").on("click",function(event){//begin on click event
//stop the link from navigated to another page
event.preventDefault();
$.ajax({//begin ajax function
url:"delete.php",//the url to your file
type:"GET",//type of http request
data:"path=" + $(this).attr("href"),//the data to send
success:function(data){//begin success
}//end success function
});//end ajax call
});//end on click event
</script>
</table>
您是否收到任何错误? – 2014-11-08 20:26:30
当页面被创建时,PHP在服务器上运行,你不能从HTML调用PHP函数。 – Barmar 2014-11-08 20:26:42
没有错误,它只是在写入并保存取消链接功能后才删除文件。 – Tom 2014-11-08 20:28:15