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 //基础类 class webSite {//一个非常简单的...

PHP/ThinkPHP实现批量打包下载文件的方法示例

前言 本文主要给大家介绍的是关于PHP/ThinkPHP实现批量打包下载文件的相关内容,分享出来供大家参考学习,话不多说了,来一起看看详细的介绍: 需求描述: 有数个文件,包含图片,文档...

PHP 截取字符串函数整理(支持gb2312和utf-8)

1、截取GB2312字符用的函数 PHP代码 复制代码 代码如下: <?php //截取中文字符串 function mysubstr($str, $start, $len) {...

SAE实时日志接口SDK用法示例

本文实例讲述了SAE实时日志接口SDK用法。分享给大家供大家参考,具体如下: 新浪SAE是新浪研发中心开发的国内首个公有云平台,从2009年开始到现在也是也来越成熟,开放了很多接口以及服...

php生成缩略图质量较差解决方法代码示例

php生成缩略图质量较差解决方法代码示例

近期弄个论坛,在首页要有个排名,显示评论最多的前十位用户的列表,列表有个略图是用户最新上传的作品,如果使用原来的图,图片过大,首页加载过慢,因此要使用略图 以上来使用imagecopyr...