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加密解密函数Authcode()修复版代码

Authcode这个函数很多人都使用,这函数来自Discuz程序,用于加密解密字符串,可以设置钥匙(key)和过期时间,在很多时候都用得着。原版的函数代码可能会生成+、/、&这样的字符,...

PHP版网站缓存加快打开速度的方法分享

PHP版网站缓存加快打开速度的方法分享

说明: 1,在服务器缓存了压缩过的文件,再次访问减少再压缩时间,降低CPU占用率。 2,通过设置客户端文件缓存时间,降低再次请求次数,可降低85%以上。 3,图片因为已经是压缩格式,只是...

如何用php获取文件名后缀

php获取文件后缀名(format file) //方法一:   复制代码 代码如下:<?php     function&nbs...

PHP输入流php://input实例讲解

对于php://input介绍,PHP官方手册文档有一段话对它进行了很明确地概述。 “php://input allows you to read raw POST data. It i...

解析thinkphp import 文件内容变量失效的问题

解析thinkphp import 文件内容变量失效的问题

用TP 集成支付宝账户绑定功能时碰上个问题ORM 下有文件 config.class.php直接import()后 发现里面的变量无法使用  但确实是加载咯。。(在config...