Ever wonder how to create a download link in PHP and then make it expire? Would you like to know how to expire a download link after a set number of times they download it? Do you want to know how to protect your files from people trying to steal your downloads? We are going to answer those questions and more on this tutorial.

Setting Up The Database For Expired Links

Set up your database first. Create a database and then use the following code to set up the table and rows. Let me tell you what each row is going to do. The link row is going to save an encrypted link you will use for your application. This will appear in the URL address bar. The file is going to be the name of the file you are uploading for a user to download. You can keep track of how many times you allow a file to be downloaded by using the counting row. The expire row is going to keep track of the time you set for the link to expire. The tstamp row is going to keep track of the time that you uploaded the file.


CREATE TABLE `links` (
`id` int(11) NOT NULL,
`link` char(40) NOT NULL,
`file` text NOT NULL,
`counting` int(11) NOT NULL DEFAULT 11,
`expire` int(11) NOT NULL,
`tstamp` int(10) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;;


For Future Projects

Keep in mind this tutorial is showing you how to do something like this. In the future, you can build on this further to create purchases or user activations on a website or app.

File To Create Expired Download Link

Here are the complete files to create expired download links in PHP. Now let’s talk about the files so you understand what they do.

CREATE EXPIRE DOWNLOAD LINK IN PHP MYSQL | BEST TUTORIAL was last modified: July 27th, 2022 by Maximus Mccullough


Pages: 1 2 3 Next


  • Boban says:

    Very good ! ! !
    How to upload multiple file and create ZIP file ?

  • Jake Roberts says:

    Mine won’t go over 100 mb and I followed to a T

    • Put this in a php tag and put it on your server [php]<?php phpinfo(); ?>[/php] that should tell you the limit for your uploads. If that is the case you will have to increase it on your server.

  • Dragos says:

    Hi!b How to delete a link from the database and how to display in webpage existing links in the database (mysql) ? Thx

    • HI Dragos. On page 2 for the download.php file you will see this
      // delete link so it can't be used again
      mysqli_query($db,"DELETE FROM links WHERE link='$link' AND counting < '1' ");

      This code is responsible for deleting the link in the database. For displaying the link you would query the database. Something like this.

      $result = $db->query("SELECT * FROM links") ;

      while ($row = $result->fetch_assoc()) {

      echo $row[‘link’];


  • Dragos says:

    I can only delete from the server the whole table of links, not each individual. I did some tests and there are links with a higher validity. I would like to be able to see all the submitted links on the index.php page and be able to delete them individually if I need to. If I try to add your script to the index.php page, I get the error:
    ( ! ) Warning: Unknown: Invalid date.timezone value ‘UTC+02.00’, we selected the timezone ‘UTC’ for now. in Unknown on line 0
    ( ! ) Parse error: syntax error, unexpected end of file in D:\wamp64\www\website\downloadlink\index.php on line 91
    downloadlink is the folder with all download from your website

  • This looks very cool! It is not exactly what I am looking for so I need to dig through it. If you could save me some time with understanding what this code is doing or point me to similar code for what I am trying to accomplish, so much the better. I’m still pretty new to PHP and other things.

    It appears as if the code takes a file referred from your own directory, then provides encryption to it before storing in a local data table with expiration and count data? Then download.php pulls the link from the table and spits it out…

    What I am working on is an e-commerce site selling books in digital format in .epub, .mobi or .zip files containing the .mp3 audio chapters.

    Both for promotional giveaways and sales, I need to provide a download link, but I do not want that link to be permanent — just open long enough for the customer to get the file before closing that path.

    So what do I need to do? I don’t need to do any uploading and I can hard code the time/date/count for downloads somewhere and change them as needed? Then use the ‘links’ data table for loading in those temporary URLs?

    I will keep working on this, but if you have any advice, it would be appeciated.


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.