-1

I want to create a download page.And I want to offer two kinds of download option for users:

->Download from fast server(users pay 1 dollar to download and download at full speed)

->Download from slow server(free but download speed should be 70-100kb/s)

I am a newbie and I will be glad if anyone can explain me how to: 1)Put a download speed limit 2)make free and premium subscription for users while keeping in mind that the free users get low speed 3)integrating paypal as a payment method for premium users Any help would be appreciated.

Thanks in advance

nishant_boro
  • 374
  • 1
  • 2
  • 8

2 Answers2

1

What you're looking for is called mod_ratelimit for Apache httpd, or the limit_rate directive for nginx.

Don't rely on PHP to handle the entire download process for you since you'd be tying up an entire PHP worker for no added benefit. Instead, you can rely on using PHP to handle the authentication mechanism of subjugating the download URI and then use X-SENDFILE headers with Apache httpd mod_xsendfile, for example, to handle the actual download.

This would be as simple as doing something like the following in your PHP...

<?php
session_start();
if ($_SESSION['authed']) {
    header("X-SENDFILE: /path/to/download/foo.zip");
} else {
    header('Location: /authenticate');
}

Some of the benefits you get from relying on the webserver to serve up the file for you include:

  • Optimal delivery (takes advantage of mmap where available)
  • Processes cache headers correctly (as if the file were served up statically)
  • Doesn't tie up your PHP process for the duration of the download, making it available to serve up other requests that really do need PHP for something other than delivering static content.
Sherif
  • 11,786
  • 3
  • 32
  • 57
  • This answer is good :) – Robert Jun 19 '15 at 08:28
  • Thanks for the quick reply..I get it..but how will I create free and paid download?..And if I apply the mod ratelimit, then wont it be applied to both download system.i.e . To slow and fast(premium users)? – nishant_boro Jun 19 '15 at 08:34
  • @hungry_dude No, which is why I've linked you to the documentation. You can set the directive to a limited path or URI. It is also possible to modify it dynamically via environment variables in Apache httpd. – Sherif Jun 19 '15 at 08:38
  • You simply just have to come up with the logic yourself to check whether the user is a premium downloader or not and set those variables accordingly for the download. One easy option is to have a symlink on the download directory and set that without the rate limit and the other with then use `X-SENDFILE` to the symlinked directory for the premium users. – Sherif Jun 19 '15 at 08:40
0

You could output file to browser in chunks and use sleep() function to limit speed. For example you output like 256 kB and then sleep(1) for 1s

Also check this link Apache - how to limit maximum download speed of files? (if not apache, i can run lighthttpd)

I think that @Sherif suggestion to use http server is better.

Community
  • 1
  • 1
Robert
  • 19,800
  • 5
  • 55
  • 85