Temporary Download Script - Unlink file after download

Ask about a PHP problem here.
Post Reply
Ehrmantraut
Posts: 26
Joined: Tue Jul 23, 2013 2:29 am

Temporary Download Script - Unlink file after download

Post by Ehrmantraut »

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
<?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 :)
Last edited by Ehrmantraut on Mon Oct 30, 2023 10:15 am, edited 1 time in total.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Temporary Download Script - Unlink file after download

Post by jacek »

Hey,

Sorry for the delay!

You could put the unlink() straight after the readfile()

readfile() should block until the file has been fully read since it returns the number of bytes read so you won't remove the file before it's downloaded.

So this should work
[syntax=php]
readfile('core/files/'. $row['file_name']);
unlink('core/files/'. $row['file_name']);
[/syntax]

You probably want it when the file has expired though?
Image
Ehrmantraut
Posts: 26
Joined: Tue Jul 23, 2013 2:29 am

Re: Temporary Download Script - Unlink file after download

Post by Ehrmantraut »

Hi Jacek,

Yes, I would prefer if the file was deleted after it had expired.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Temporary Download Script - Unlink file after download

Post by jacek »

To do it once the time was reached you'd need a scheduled task to run every hour or so to check for expired files and remove them.

I'm not sure how many hosting providers provide that sort of thing though :/

You'd essentially need to do a query for all expired files
[syntax=sql]SELECT stuff FROM files WHERE file_expiry < UNIX_TIMESTAMP()[/syntax]

Then loop over those files and remove them from the system
[syntax=php]while ($row = mysql_fetch_assoc($result)){
unlink('core/files/'. $row['file_name']);
}[/syntax]

Finally delete all of the files from the database.
[syntax=sql]DELETE FROM files WHERE file_expiry < UNIX_TIMESTAMP()[/syntax]

You could do the delete in the loop too if that's easier, it would be much less efficient though and might get slow if there are a large number of files.
Image
Post Reply