php控制文件下载速度的方法

yipeiwu_com6年前PHP代码库

本文实例讲述了php控制文件下载速度的方法。分享给大家供大家参考。具体实现方法如下:

<?php
 /*
 * set here a limit of downloading rate (e.g. 10.20 Kb/s)
 */
 $download_rate = 10.20;
 $download_file = 'download-file.zip'; 
 $target_file = 'target-file.zip';
 if(file_exists($download_file)){
  /* headers */
  header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
  header('Cache-control: private');
  header('Content-Type: application/octet-stream');
  header('Content-Length: '.filesize($download_file));
  header('Content-Disposition: filename='.$target_file);
  /* flush content */
  flush();
  /* open file */
  $fh = @fopen($download_file, 'r');
  while(!feof($fh)){
   /* send only current part of the file to browser */
   print fread($fh, round($download_rate * 1024));
   /* flush the content to the browser */
   flush();
   /* sleep for 1 sec */
   sleep(1);
  }
  /* close file */
  @fclose($fh);
 }else{
  die('Fatal error: the '.$download_file.' file does not exist!');
 }
?>

希望本文所述对大家的php程序设计有所帮助。

相关文章

php实现的网页版剪刀石头布游戏示例

php实现的网页版剪刀石头布游戏示例

本文实例讲述了php实现的网页版剪刀石头布游戏。分享给大家供大家参考,具体如下: <?php /* * Created on 2016-11-25 * */ i...

PHP7内核CGI与FastCGI详解

PHP7内核CGI与FastCGI详解

CGI:是 Web Server 与 Web Application 之间数据交换的一种协议。 FastCGI:同 CGI,是一种通信协议,但比 CGI 在效率上做了一些优化。 PHP-...

基于php常用正则表达式的整理汇总

如下所示:复制代码 代码如下:"^/d+$"  //非负整数(正整数 + 0) "^[0-9]*[1-9][0-9]*$"  //正整数 "^((-/d+)|(0+))$"  //非正整...

php中利用post传递字符串重定向的实现代码

复制代码 代码如下: $ch = curl_init('http://domain-name.com/page.php');       curl_seto...

PHP使用文件锁解决高并发问题示例

本文实例讲述了PHP使用文件锁解决高并发问题。分享给大家供大家参考,具体如下: 新建一个.txt文件,文件中什么都不用写。 【一】.阻塞(等待)模式:(只要有其他进程已经加锁文件,当前进...