Temporary Download Script - Unlink file after download
Posted: Thu May 05, 2016 3:34 pm
Hi Folks,
Quick question: How would I go about using the unlink(); function to delete files from the 'files' folder after a user has downloaded the file?
Download.php File
Quick question: How would I go about using the unlink(); function to delete files from the 'files' folder after a user has downloaded the file?
Download.php File
<?php
include('core/inc/init.inc.php');
if (isset($_GET['file_id'])) {
$file_id = (int) $_GET['file_id'];
$file = mysql_query("SELECT file_name, file_expiry FROM files WHERE file_id={$file_id}");
if (mysql_num_rows($file) != 1) {
echo "Sorry, the file you have requested does not exist or has been deleted.";
} else {
$row = mysql_fetch_assoc($file);
if ($row['file_expiry'] < time()) {
echo "Sorry, This file has now expired. Please <a href='mailto:#?subject=Expired File'>contact</a> the administrator for more details.";
} else {
header('Content-Description: File Transfer');
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"{$row['file_name']}\"");
header('Expires: 0');
header('Pragma: no-cache');
header('Content-Length: ' . filesize('core/files/'.$row['file_name']));
readfile('core/files/'. $row['file_name']);
}
}
$file = mysql_query("SELECT file_name, file_expiry FROM files WHERE file_id={$file_id}");
$row = mysql_fetch_assoc($file);
$expiry = $row['file_expiry'];
if (time() > $expiry)
mysql_query("DELETE FROM `files` WHERE `file_id`=" . $file_id);
}
?>
Any help would be much appreciated