Giới hạn tốc độ download bằng php

Giới hạn tốc độ download bằng php

/**
* Download with limited speed class file.
*
* i-php group :
* Copyright (c) <2008>,
* Licensed under New BSD License
* @filesource download_limited.php
* @copyright Copyright 2008, i-php group
* @author [email protected]
* @link http://i-php.net/about/
* @since php 5.0
* @version $Revision: 1 $
* @modifiedby $LastChangedBy: habogay $
* @lastmodified $Date: 2008-08-18 09:45:49 -0500 (Sat, 18 Oct 2008) $
* @license http://www.freebsd.org/copyright/freebsd-license.html New BSD Style License
*/

class DownloadLimited
{
/**
* Send the local file to client with limit download rate as user view file name
*
* @param string $localFile file to send
* @param string $userViewFileName filename client will see
* @param float $downloadRate download limit speed in kb/s (ex: 50 kb/s)
* @return no return
*/
function sendFile($localFile,$userViewFileName,$downloadRate)
{
/** check file exists
* @see file_exists
*/
if(!file_exists($localFile))
{
throw new Exception('File not found execption file :'.$localFile.' not found');
}
/** check file is file
* @see is_file
*/
if(!is_file($localFile))
{
throw new Exception($localFile.' not is a file');
}
if(!is_readable($localFile))
{
throw new Exception('file '.$localFile.' not readable ');
}
// send headers
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($localFile));
header('Content-Disposition: filename='.$userViewFileName);

// flush header content
flush();
// open file stream
$file = fopen($localFile, "r");
while(!feof($file))
{

// send the current file part to the browser
print fread($file, round($downloadRate * 1024));

// !important flush the content to the browser
flush();

// !important sleep one second
sleep(1);
}

// close file stream
fclose($file);


}
/**
* Send the string data to client with limit download rate as user view file name
*
* @param string string data to send include ascii,utf-8 character
* @param string $userViewFileName filename client will see
* @param float $downloadRate download limit speed in kb/s (ex: 50 kb/s)
* @return no return
*/
function sendString($data,$userViewFileName,$downloadRate)
{
// send headers
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.strlen($data));
header('Content-Disposition: filename='.$userViewFileName);
// flush header content
flush();

$start=0;
$sendSize=0;
while($sendSize    {
$sendSize=round($downloadRate*1024);
print substr($data,$start,$sendSize);
$start=$sendSize;
// !important flush the content to the browser
flush();

// !important sleep one second
sleep(1);
}
}

}
?>
Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Bình luận (0)

 

Add Comment

Filtered HTML

  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Các thẻ HTML được chấp nhận: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Tự động ngắt dòng và đoạn văn.

Plain text

  • No HTML tags allowed.
  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Tự động ngắt dòng và đoạn văn.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
galaxy

Những thiết bị vi phạm của Samsung trong vụ kiện với Apple

Rõ ràng điều này có ảnh hưởng không nhỏ đến uy tín cũng như các sản phẩm của hãng điện tử Hàn Quốc. Sau đây chúng ta sẽ cùng điểm qua 25 thiết bị vi phạm của Samsung trong vụ kiện tụng với Apple.

Error

Những lỗi thường gặp của Keyword Research

Sau nhiều tháng ngày làm SEO vất vả và cuối cùng thì không đạt được những gì mình mong muốn. Sẽ có nhiều câu hỏi đặt ra, vì sao giá click của tôi quá thấp, vì sao tôi đã ở Top 1 nhưng quá ít visit ?

Lỗi hao pin iPhone 4S được Apple khắc phục

Lỗi hao pin iPhone 4S được Apple khắc phục

Apple đã chính thức đưa ra phiên bản iOS 5.0.1 để sửa lỗi hao pin mà người dùng gần đây phàn nàn trên các diễn đàn mạng.

Công ty diệt chuột T&C

 

Diet con trung