Skip to content

Instantly share code, notes, and snippets.

@xprilion
Forked from psykzz/downloader.class.php
Last active March 18, 2018 06:28
Show Gist options
  • Save xprilion/4afb1950eed61b5a86a8bdd2f217134d to your computer and use it in GitHub Desktop.
Save xprilion/4afb1950eed61b5a86a8bdd2f217134d to your computer and use it in GitHub Desktop.
Simple rate limited downloader with example
<?php
/* *
* @filename downloader.class.php
* @author PsyKzz
* @edited_by xprilion
* @version 2.0.0
* @description Simple class to rate limit your downloads, while also providing a custom tickrate to combat timeout issues.
* @url http://www.psykzz.co.uk
* @url https://xprilion.com
*
* @license 'WTFPL ' - "Do What The Fuck You Want To Public License".
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details. */
class Downloader {
private $file_path;
private $downloadRate;
private $file_pointer;
private $error_message;
private $_tickRate = 4; // Ticks per second.
private $_oldMaxExecTime; // saving the old value.
function __construct($file_to_download = null) {
$this->_tickRate = 4;
$this->downloadRate = 1024; // in Kb/s (default: 1Mb/s)
$this->file_pointer = 0; // position of current download.
$this->setFile($file_to_download);
}
public function setFile($file) {
if (file_exists($file) && is_file($file))
$this->file_path = $file;
else
throw new Exception("Error finding file ({$this->file_path}).");
}
public function setRate($kbRate) {
$this->downloadRate = $kbRate;
}
private function sendHeaders() {
if (!headers_sent($filename, $linenum)) {
header("Content-Type: application/octet-stream");
header("Content-Description: file transfer");
header('Content-Disposition: attachment; filename="' . $this->file_path . '"');
header('Content-Length: '. $this->file_path);
} else {
throw new Exception("Headers have already been sent. File: {$filename} Line: {$linenum}");
}
}
public function download() {
if (!$this->file_path) {
throw new Exception("Error finding file ({$this->file_path}).");
}
flush();
$this->_oldMaxExecTime = ini_get('max_execution_time');
ini_set('max_execution_time', 0);
$file = fopen($this->file_path, "r");
while(!feof($file)) {
print fread($file, ((($this->downloadRate))/$this->_tickRate));
flush();
usleep((1000/$this->_tickRate));
}
fclose($file);
ini_set('max_execution_time', $this->_oldMaxExecTime);
return true; // file downloaded.
}
}
/*
Sample Usage:
include('downloader.class.php');
$dfile = new Downloader($downloadPath);
$dfile->setRate(100);
$dfile->download();
*/
@Reetika12795
Copy link

made it public :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment