How to Create One Time Temporary Download Link with Expiration in PHP

Tutorials Website

Temporary download link is extremely helpful to offer Digital products on the site. It gives a safe method to share the download link of the advanced items. The client permits downloading the record just a single time, after the download the link is terminated or evacuated. The one-time download link is perfect to give an advanced item (code, music, video, and so on) to a solitary individual and terminate the link once the item is downloaded.

With the single-utilize download link, you don’t have to physically screen the download action keeping in mind the end goal to change the download link. Rather, the download link will be terminated quickly after the primary download. In this instructional exercise, we will demonstrate to you proper methodologies to produce one-time download link in PHP and execute the temporary download URL usefulness on the web application utilizing PHP.

The illustration code enables you to create a one of a kind link to download the document from the server. This link will enable the client to download one time. Likewise, the link will have a termination time and it will be lapsed after the predefined expiry date.

For instance, you need to offer an eBook on your site. The eBook is sold on your site for $5, you could utilize our content to enable that client to download the separate eBook just a single time. The download link will give them a predetermined number of seconds/minutes/hours/days/week/years to assert their download.

The following procedure will be taken to implement the temporary download link functionality in PHP.

  • Create a download link with a special key.
  • Make a secured directory to store keys.
  • Make a file and write a one of a unique key to it.
  • On download ask for, token key and lapse time is approved.
  • Power the program to download the record.

Setups (config.php)

The setup factors are characterized in this document.

  • $files – A variety of the records with the one of a kind ID.
  •  You can indicate the distinctive name of the record being downloaded. It secures the first document.
  • You can determine the nearby or remote record way.
  • BASE_URL – Define the URL of the application.
  • DOWNLOAD_PATH – Define the way of the document downloading content.
  • TOKEN_DIR – Set the token directory way where the keys will be put away.
  • OAUTH_PASSWORD – Set the verification secret word to produce a download link.
  • EXPIRATION_TIME – Set a period when the document will terminate.

index.php

In this document, a link will be shown to explore to the download link creation record. The confirmation secret key should be indicated in the inquiry string of the link.

Make Temporary Download Link (generate-link.php)

This document makes a temporary download link and records the links on the page. The inquiry string must have the verification watchword and should be coordinated with the predefined in the config.php document, generally, 404 mistake is rendered.

  • Get the validation secret word from the question string.
  • Approve the validation secret word.
  • Encode the record ID with base64_encode() in PHP.
  • Produce another interesting key with a timestamp utilizing uniqid() in PHP.
  • Create download link with record ID and key.
  • Make a secured directory to store keys.
  • Write the key in a record and place it in the token directory.
  • List all the download links in the page.

Download File by Temporary Link (download-link.php)

This document downloads the record by the temporary download link.

  • Get the document ID and key from the question string of the URL.
  • Get the time from the key and figure link termination time.
  • Recover the keys from the tokens record.
  • Circle through the keys to discover a match, when the match is discovered, evacuate it.
  • Put the rest of the keys again into the tokens record.
  • if match found and the link isn’t expired

Conclusion

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

[sociallocker]

[/sociallocker]

Related posts