Generating Dynamic Urls For Downloading Files in PHP

Generating Dynamic Urls For Downloading Files in PHP

Want to create a download server for movies or you want to allow your users to buy digital downloads but you don’t want to expose the real path for the file so the solution is to create dynamic urls.

 

 

Dynamic urls is a url that changed every time when the user want to download a certain file, instead of giving the user the actual file path. This helps protecting the file sources from being stolen as this may be a paid file and also decrease the heavy load on the server.

Imagine you own a website that sell digital books, of course the process is the user pay for the book first then he can download the file by sending some kind of email message with the download link. To download the file the system generate a dynamic url for that file and for that user.

Dynamic urls takes the form of a normal link and ends and with a long token string, this token is the download identifier and this marks the url as dynamic and it has an expiration time and in some websites is attached with the device IP.

In this tutorial i will create a simple php script to show the process of dynamic url generation, we will create two tables. The first table is the table that holds the actual file names, and the second table is a table that holds the dynamic urls.

 

Database

Let’s create a new database and name it whatever you want then execute those queries:

As you see the files table only holds the filename and created_at fields. I assume that the files stored in a fixed location in the project.

The file_urls table contains the file_id, token_identifier and is unique, time_before_expire and the ip_address. time_before_expire field this is the expiration time for the url after that time that url is useless and inaccessible. The token_identifier must be unique per download.

 

Preparing Code

Create a new folder in your server root directory name it whatever you want, in my case i called it downloader with those files:

 

downloader

     – files/

     – scripts/

              —- scripts.js

     —- includes.php

     —- index.php

     —- download.php

     —- fetch_link.php  

     —- download-box.php

     —- d.php

 

Before starting to create the actual code try to add some files in the files/ directory and also insert them into the database like this:

dynamic files download

 

Let’s open the includes.php and insert the below code:

The includes.php contains database config and some constants such as the expiration time and the files location and also i have added the code that connects to the database using PHP PDO.

The file  contains some helper functions that we will need across the project like getFiles() function for displaying the files, and getFile($id) to retrieve a file by id, etc.

 

Displaying files

The first thing we need to do is to display the files to user so it can select a file and download it. So open index.php and modify it with this code:

index.php

In this simple script i iterate over the files returned by getFiles() function and display download links, in each link the user will be redirected to ./download.php.



Downloading files

Now open download.php and insert this code

In the above code first i check the file using checkId() function then i check also if their are a file url for this device IP that it is not expired using getFileUrl() function in this snippet:

If there is a working file url we display it in the download box along with the remaining time to expire. Otherwise we display some sort of counter that counts from 10 downward and on reaching 0 it displays the download url using ajax.

 

download-box.php

The download box displays the remaining time in hours for the link to terminate and actual download url with the token identifier.

 

scripts/scripts.js

The above javascript code displays a counter to user that decrements on every second and on reaching 0 it calls fetchDownloadLink() function which sends an ajax request to fetch the new download link.

 

fetch-link.php

Here we just insert a new url using insertFileUrl() function which uses another function to generate a random token string called randStr(). Finally we return the download box with the new url.



d.php

The above code allows downloading the file using php header() function. At the beginning of the function we retrieve the file by token using getFileUrlByToken() function.

next we check if there is a file that match this identifier and also we check if the url expiration time not ended. Finally we check if the file already exits and download it using this snippet:

 

Now try to run the code, you should see something like in those screenshots:

generating dynamic urls with php

generating dynamic urls with php

generating dynamic urls with php

Share this: