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: