简体   繁体   中英

Create dynamic, expireable and resumable download link using php, apache

Here is the problem details:

1) I want to create dynamic (ip based) download link. So user can't download the file with different IP with the same download link.

2) Before start the actual download, i want to log this download request using php and perform some checks (verify the http referrer) to allow the user to download the actual file.

3) I also want the download file to be resumable and could be downloaded with download manager (with multiple download instances). Also want to limit the maximum number of allowed instances for each download.

4) The file size could be more than 200 MBs.

So, the solution which i am thinking is to create the download link with the md5 hash of user's ip. Eg http://yourdomain.com/download.php?ip_hash=hash-of-the-ip&file=file-to-download

This is just a example but we can also create a nice link of this using htaccess.

What should i do next? I tried to do it using

header("Content-Type: $ctype");
header("Content-Length: " . filesize($file));
header("Content-Disposition: attachment; filename=\"$fileName\"");
readfile($file);

But using this the download does not remain resumable for the end user.

Is this fine to send large files using this method?

After doing some research, I came to know that the .exe files become corrupted for the end user using this way.

After doing some more research, i have found the answer of my question. I just thought i should share it with you guys as well.

As rambo commented, we can use mod_xsendfile module of the apache server. We need to enable it if its disabled.

Here is the link to download the module files if your apache does not have this module. Its available for mostly all the versions of apache and available for both x32 and x64.
https://github.com/nmaier/mod_xsendfile

You can use the following code to send the file using this apache module after doing all your custom validations.

<?php
//We want to force a download box with the filename hello.txt
header('Content-Disposition: attachment;filename=hello.txt');

//File is located at data/hello.txt
header('X-Sendfile: data/hello.txt');
?>

I hope it will help you guys :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM