2007-09-01, 12:33 | Link #1 |
Chanchanchan
Join Date: May 2006
Location: Kuurne (Belgium)
Age: 34
|
Daily bandwidth limit (php) script?
(don't know if it's the right section, but couldn't figure it would fit in anywhere else)
hey, I've been searching everywhere to get my hands on a easy script that allows to set a daily download-limit for large files on my webserver. Anyone has such a script and willing to share it? ^^ |
2007-09-01, 22:06 | Link #3 |
AS Oji-kun
Join Date: Nov 2006
Age: 74
|
You need to write a PHP wrapper that keeps track of the number and sizes of the files downloaded. (The simplest way would be to write running totals to a text file in a directory to which the web server user has rights.) When the limits are exceeded the wrapper displays a "sorry, no more today" message. I'd have it record the time of day in the file as well; the first time the script is accessed after midnight you'd just zero the totals.
I've used a wrapper like this to limit file downloads to authorized users. Here's a code snippet: Code:
<?php ### NO TEXT CAN BE ECHOED BEFORE HERE ### ### SEND HEADERS ### header("Content-Type: ".$thisdoc["mimetype"]); header("Content-Disposition: inline; filename=".$thisdoc["filename"]); ### Open the file and display it $fp=fopen("/path/to/".$thisdoc["filename"],"r"); fpassthru($fp); fclose($fp); ?> I usually manage this stuff by storing the information about the files in a database (PostreSQL is my choice), but that might be overkill for you. An easier solution is to write the download URIs in a form like "/getfile.php?name=myfile.ext" and have the script use the file's extension to determine which mimetype to send in the HTTP headers. A simple PHP array like : Code:
$mimetypes=array("mkv"=>"application/x-matroska","zip"=>"application/x-zip",etc.) I leave the rest as an exercise for the reader.
__________________
Last edited by SeijiSensei; 2007-09-02 at 10:33. Reason: fixed typo in array definitions |
Thread Tools | |
|
|