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

yipeiwu_com5年前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实现自动回复了。 index.php中的代码 <?php...

php Xdebug的安装与使用详解

php Xdebug的安装与使用详解

为什么需要Debugger?很多PHP程序员调试使用echo、print_r()、var_dump()、printf()等,其实对 于有较丰富开发经验的程序员来说这些也已经足够了,他们往...

PHP下escape解码函数的实现方法

GB2312编码: 复制代码 代码如下: function unescape($str) { $str = rawurldecode($str); preg_match_all("/%u...

PHP中串行化用法示例

本文实例讲述了PHP中串行化用法。分享给大家供大家参考,具体如下: 功能:串行化用于对对象的存储或者传输,通过反串行化得到这个对象。 1. Person.class.php: <...

PHP提示Notice: Undefined variable的解决办法

PHP默认配置会报这个错误,我的PHP版本是5.2.13,存在这个问题: Notice: Undefined variable 这就是将警告在页面上打印出来,虽然这是有利于暴露问题,但实...