Đề mục chính
| | |
Cấu trúc diễn đàn
| | |
Thống kê
| |
Đề tài: 10641
Bài gửi: 12205
Thành viên: 20,207
Thành viên tích cực: 81 |
Xin cùng nhau chào đón thành viên mới nhất: buixuantu |
Kỷ lục: 624 người đã ghé thăm 17/11/2010 lúc 06:16 AM.
| Số người đang xem
| | | |  | 
01/03/2010, 11:57 PM
| [PHP] Giới hạn Tốc độ download bằng php - LIMIT Download | Admintalk's Staff can't stop talking
Bài gởi: 4,003
My Mood: Thanks: 27
Thanked 210 Times in 152 Posts
VP: 0.00 Donate | PHP Code: <?php
/**
* Download with limited speed class file.
* Licensed under New BSD License
* @filesource download_limited.php
* @since php 5.0
* @version $Revision: 1 $
* @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<strlen($data))
{
$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);
}
}
}
?> [/quote] Bài viết cùng chủ đề: |
| |  | |
Ðang đọc: 1 (0 thành viên và 1 khách) | | | | Ðiều Chỉnh | Kiếm Trong Bài | | | | | Xếp Bài | Linear Mode |
Quyền Sử Dụng Ở Diễn Ðàn
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML đang Tắt | | | |