deleting file with unlink()
Posted: Thu Dec 30, 2010 10:54 am
the code below creates a table that lets me choose a row to delete from mysql database( it all works no problems)
but what i am trying to do is add unlink() to the script so when i delete a row it deletes the linked file.
this bit is where the filename is shown like test.doc and the files are stored in the directory /test_docs
is there anyway to do it so it deletes the file
Code: Select all
<?php
$host="host"; // Host name
$username="user"; // Mysql username
$password="pass"; // Mysql password
$db_name="testdocs"; // Database name
$tbl_name="Docs"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Build SQL query
if(!isset($_POST['delete'])) $sql="SELECT * FROM $tbl_name ORDER BY id";
else {
$sql = "DELETE FROM $tbl_name WHERE";
// add row id to where section
for($i=0;$i<count($_POST['checkbox']);$i++){
if($i != 0) $sql.= "AND ";
$sql .= " id='" . $_POST['checkbox'][$i] . "'";
}
}
$result = mysql_query($sql);
if(isset($_POST['delete'])) header('Location: index.php'); // redirect
?>
<table align="center" width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="delete" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<table width="400" border="1" cellpadding="3" cellspacing="1">
<tr>
<td colspan="5" align="center"><strong>Testimonials</strong> </td>
</tr>
<tr>
<td align="center"><strong>Select</strong></td>
<td align="center"><strong>ID</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Message</strong></td>
<td align="center"><strong>Download</strong></td>
</tr>
<?php while($rows=mysql_fetch_array($result)){ ?>
<tr>
<td align="center">
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>">
</td>
<td><? echo $rows['id']; ?></td>
<td><? echo $rows['Name']; ?></td>
<td><? echo $rows['Message']; ?></td>
<td><? echo $rows['Download']; ?></td>
</tr>
<tr>
<td colspan="5" align="center"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php }
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>this bit is where the filename is shown like test.doc and the files are stored in the directory /test_docs
Code: Select all
<td><? echo $rows['Download']; ?></td>