Index File To Start Process
The index.php file is there to start the process. You can name it anything you want. It looks like this when you open it to your editor. See the video for detailed explanations.
<?php include('header.php'); if(isset($_POST['submit'])){ $errors= array(); $file_name = $_FILES['file']['name']; $file_size =$_FILES['file']['size']; $file_tmp =$_FILES['file']['tmp_name']; $file_type=$_FILES['file']['type']; $fileend=explode('.',$file_name); $file_ext=strtolower(end($fileend)); $extensions= array("jpeg","jpg","png","pdf", "zip"); if(in_array($file_ext,$extensions)=== false){ $errors[]="extension not allowed, please choose a JPEG, PNG, ZIP or PDF file."; } if($file_size > 2097152){ $errors[]='File size must be under 2 MB'; } if(empty($errors)==true){ move_uploaded_file($file_tmp,"files/".$file_name); //echo "Success"; }else{ print_r($errors); } $expire=$_POST['date']; $counting=$_POST['counting']; $date = date('M d, Y h:i:s A', strtotime($expire)); $dbdate = date('Y M d H:i:s', strtotime($expire)); $one= 'To Expire on '.$date.'<br/>'; $d = DateTime::createFromFormat( 'Y M d H:i:s', $dbdate, new DateTimeZone('EST') ); if ($d === false) { die("Incorrect date string"); } else { $expiredate=$d->getTimestamp(); } $link = sha1(uniqid($file_name, true)); $tstamp=$_SERVER["REQUEST_TIME"]; mysqli_query($db,"INSERT INTO links(`link`,`file`, `counting`, `expire`, `tstamp`) VALUES ('$link', '$file_name', '$counting','$expiredate','$tstamp')"); $two= '<a href="download.php?link='.$link.' " target="_NEW">Link</a>'; } ?> <div class="container"> <div class="jumbotron"><p class="text-xl-center"><?php if(isset($one)){echo $one.$two;};?></p></div> <h1 class="animated bounce"><span class="glyphicon glyphicon-link"></span>Generate A Link That Expires</h1> <div class="row"> <div class="col-sm-4"></div> <div class="col-sm-4"> <form method="post" role="form" enctype="multipart/form-data"> <div class="form-group"> <label for="file">Select File:</label> <input type="file" class="form-control" name="file" required> </div> <div class="form-group"> <label for="counting">How Many Times Can Link Be Accessed?:</label> <input type="tel" class="form-control" name="counting" required> </div> <div class="form-group"> <label for="date">Set Expiration Date and Time For Link:</label> <input type="datetime-local" class="form-control" name="date" required> </div> <input type="submit" name="submit" class="btn btn-success btn-lg" value="submit" /> </form> </div> <div class="col-sm-4"></div> </div> </div> <?php include('footer.php'); ?>
Now lets go to the download file on the next page.
CREATE EXPIRE DOWNLOAD LINK IN PHP MYSQL | BEST TUTORIAL was last modified: July 27th, 2022 by
12 Comments
Hi
Very good ! ! !
How to upload multiple file and create ZIP file ?
Ill have to do that in a future tutorial.
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.
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’];
}
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
OK The issue was that the database was not auto incrementing on the ID I fixed that database structure. Here is a sample of the mysql database. You can import this via phpMyAdmin. Then each entry will have a unique ID.
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.
Thanks
Let me know if you were able to get anywhere with that. I’ll go over your questions as soon as I can and get you some answers. 🙂
Using MAC but on Chrome, but when I upload a photo .jpg or .png and I click on the generated link, it doesn’t download directly it opens a page that shows code. Any ideas?
Try this. I uploaded files to a test server. The link is here. https://a1sold.com/test/expired/ The file that I uploaded is here. It is suppose to expire tomorrow. https://a1sold.com/test/expired/download.php?link=45dc8f36960c2f4f182c58ef4bfd6be630e47577 Try that in chrome and see what happens. 🙂