One Time Use Download Link
When you need to provide a temporary download link that can be used only once, the PHP script in this article can provide the means.
With the download limited to once, there is no longer need to manually monitor activity in order to remove the download file immediately afterward. Instead, the download file can be removed later at your leisure.
Alternatively, the file can be deleted from the server immediately after downloading, instead of renamed.
How the "Use Once" Download Link Works
The download file is in a directory never revealed to the person doing the downloading.
When a download has been completed, the download file on the server is renamed.
If a subsequent download is attempted, the PHP script will be unable to find the requested file because the file was renamed or deleted.
The PHP script allows you to specify the directory where the downloadable files are located.
The script also allows to you to specify the characters used to rename the file. If nothing is specified here, the downloadable file is deleted from the server instead of renamed.
The Single Download PHP Script
Here is the PHP script. Copy it and save it to your hard drive as SingleDownload.php or other appropriate file name.
<?php # This must be the first line in the file, no preceding space or other characters. /* Single Download Version 1.0 February 8, 2010 Will Bontrager https://www.willmaster.com/ Copyright 2010 Bontrager Connection, LLC Bontrager Connection, LLC grants you a royalty free license to use or modify this software provided this notice appears on all copies. This software is provided "AS IS," without a warranty of any kind. */ // Two places to customize: // // Place 1 - // Specify the directory containing the downloadable file. // Specify directory as relative to the document root // directory (the directory where the domain's main/index // web page file is located). The directory may need 777 // permissions so script can rename files in it. $DownloadableFilesDirectory = "/secret"; // Place 2 - // Specify the characters to prepend to the file name when // the file is renamed after being downloaded. For example, // if "HelloThere_" is specified here and the downloadable // file is named myfile.zip, then the file will be renamed // as HelloThere_myfile.zip after it is downloaded. // NOTE: If nothing is specified here, the file will be // deleted from the server after it has been downloaded, // instead of renamed. $FileRenamePrependCharacters = "HelloThere_"; /* No other customizations required. */ if( ! empty($FileRenamePrependCharacters) ) { $FileRenamePrependCharacters = ltrim($FileRenamePrependCharacters); } $DownloadableFilesDirectory = preg_replace('/^\/*/','/',$DownloadableFilesDirectory); $DownloadableFilesDirectory = preg_replace('/\/*$/','',$DownloadableFilesDirectory); $Directory = $_SERVER['DOCUMENT_ROOT'].$DownloadableFilesDirectory; if( empty($_GET['download']) ) { exit; } if( empty($_GET['savefile']) ) { $_GET['savefile'] = $_GET['download']; } $Dfile = $Directory.'/'.$_GET['download']; $size = filesize($Dfile); if( ! $size ) { echo '<p><b>The download file is empty or was not located.</b></p>'; exit; } $ctype = 'application/octet-stream'; header('Cache-control: private'); header("Content-Type: $ctype"); header('Content-Disposition: attachment; filename="'.$_GET['savefile'].'"'); header('Content-Transfer-Encoding: binary'); header("Content-Length: $size"); @readfile($Dfile); if( empty($FileRenamePrependCharacters) ) { unlink($Dfile); } else { $pieces = explode('/',$Dfile); $pieces[count($pieces)-1] = $FileRenamePrependCharacters . $pieces[count($pieces)-1]; rename($Dfile,implode('/',$pieces)); } exit; ?>
The PHP script has two places to customize.
-
Specify the directory containing the downloadable file.
Specify the directory as relative to the document root directory. The document root directory is where the domain's main or index web page file is located.
The directory may need 777 permissions so script can rename files in it. Test it first. If the file is not renamed or deleted after being downloaded, then give the directory 777 permissions.
-
Specify the characters to prepend to the file name when renaming.
For example, if you specify "DONE_" and the downloaded file is myfile.pdf, then it will be renamed to DONE_myfile.pdf after it is downloaded.
If nothing is specified here, the file on the server will be deleted instead of renamed.
Using the Software
Upload a file to the directory specified in the PHP script.
To download, use the URL of the PHP script appended with "?download=FILENAME".
For example, if the file to download is named myfile.zip, then the URL would be something like this:
With the above link, the browser will suggest the file name to save on the person's hard drive to be the same as the file name being downloaded.
To suggest a different file name to save on the person's hard drive, append "&savefile=FILENAME" to the URL. Example:
With the above URL, the file being downloaded is the one named myfile.zip on the server. It is downloaded to be saved as yourfile.zip.
Whenever a file is downloaded, the file on the server is either renamed or deleted depending on your customization.
Will Bontrager