我使用一个上传文件到文件夹和信息(名称,大小)到数据库的表格。所以我想删除从数据库删除文件和数据的表格。
<?php
include '../dbc.php';
$tbl_name="image"; // Table name
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
//detect file
$directory = "../../Upload/";
$images = scandir($directory);
$ignore = Array(".", "..");
?>
<table width="800px" style="background:white;margin:50px auto; font-size:13px;" border="1" cellpadding="3">
<tr>
<td colspan="3"><strong>Delete:</strong> </td>
</tr>
<tr>
<td width="40%" align="center"><strong>Name</strong></td>
<td width="10%" align="center"><strong>Size</strong></td>
<td width="5%" align="center"><strong>Delete</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr id='del'>
<td><?php echo $rows['name']; ?></td>
<td><?php echo number_format($rows['imgSize'], 2) ?> MB</td>
<td><a name="delete" href="delData1.php?id=<?php echo $rows['imgId'];
?>">Delete</a></td>
</tr>
<?php
}
?>
</table>
<?php
mysql_close();
?>
delData1.php
<?php
include '../dbc.php';
$tbl_name="image"; // Table name
// get value of id that sent from address bar
$id=$_GET['id'];
$filename = $_POST['filename'];
$path = $_POST['directory'];
if(file_exists($path."/".$filename)) {
unlink($path."/".$filename); //delete file
}
// Delete data in mysql from row that has this id
$sql="DELETE FROM $tbl_name WHERE imgId='$id'";
$result=mysql_query($sql);
// if successfully deleted
if($result){
rmdir('$name');
echo "Deleted";
echo "<BR>";
header("Location:Data.php");
}
else {
echo "ERROR";
}
?>
它从数据库中删除数据,但文件保留在文件夹中。