Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Automation

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

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.

  1. 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.

  2. 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:

http://example.com/SingleDownload.php?download=myfile.zip

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:

http://example.com/SingleDownload.php?download=myfile.zip&savefile=yourfile.zip

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

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC