I have the following code which deletes a record from a database. However I would like it also to remove an associated file from the server. One of the columns contains the path to the file. Can someone explain how I would use the unlink function here?
<?php
include('config.php');
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
$id = $_GET['id'];
$result = mysql_query("DELETE FROM images WHERE id=$id")
or die(mysql_error());
header("Location: view.php");
}
else
{
header("Location: view.php");
}
?>
Before anything else, fix your SQL injection holes.
To answer your question, first you have to select the record to details about that local file. Once you've nuked the database record, you've lost the only place where the location of the file is stored, so get that data first.
That's make it, in pseudo-code:
You can just do a query first to retrieve the imagename and after that simply delete the file:
unlink($theFilenameRetrievedFromDb);
You have to first make a select statement to your database to know the path of your file. Once this is done, you can use unlink on this path, then make your delete statement.