PDA

View Full Version : راهنمایی در مورد استفاده از یک class



alih110
پنج شنبه 23 شهریور 1391, 19:47 عصر
سلام دوستان
میخواستم در مورد استفاده از این class به من کمک کنید
نمیدونم چه طوری باید این رو فراخوانی و استفاده کنم
ممنون از شما

Hr.Ziggurat
جمعه 24 شهریور 1391, 11:59 صبح
سلام ، کد این کلاس یکم ایراد داشت که ردیفش کردم :


class Downloader {
private $file_path;
private $downloadRate;
private $file_pointer;
private $error_message;
private $_tickRate = 4;
private $_oldMaxExecTime;
function __construct($file_to_download) {
$this->_tickRate = 4;
$this->downloadRate = 1024; // in KB/s (default: 1MB/s)
$this->setFile($file_to_download);
}
public function setFile($file) {
if (file_exists($file) && is_file($file))
$this->file_path = $file;
else
die("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: '. filesize($this->file_path));
} else {
die("Headers have already been sent. File: {$filename} Line: {$linenum}");
}
}
public function download() {
$this->sendHeaders();
if (!$this->file_path) {
die("Error finding file ({$this->file_path}).");
}
flush();
if(ini_get('safe_mode'))
die("Error! Safe Mode On The Server Has To Be Off");
set_time_limit(0);
if($this->downloadRate > 8 * 1024 * 1024)
die("Error! Download Rate Couldn't Be More Than 8MBPS!");
$file = fopen($this->file_path, "rb");
while(!feof($file)) {
print fread($file, ($this->downloadRate));
flush();
usleep((1000/$this->_tickRate));
}
fclose($file);
return true;
}
}


اینم نحوه ی استفاده از این کلاس :


<?php
include("downloader.class.php");
$dc = new Downloader("file patch");
$dc->setRate(100); // Set download rate to 100 Kbyte/s
$dc->download();
?>