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 CLI模式下的多进程应用分析

PHP在很多时候不适合做常驻的SHELL进程, 他没有专门的gc例程, 也没有有效的内存管理途径. 所以如果用PHP做常驻SHELL, 你会经常被内存耗尽导致abort而unhappy....

CentOS系统中PHP安装扩展的方式汇总

CentOS系统中PHP安装扩展的方式汇总

CentOS 下,PHP有多种方式来安装扩展, 主要有 包管理式 的 yum 安装 、 pecl 安装 , 以及 源码编译安装 。 包管理式 的安装卸载尤为方便,而 源码编译式 的安装则...

PHP判断密码强度的方法详解

本文实例讲述了PHP判断密码强度的方法。分享给大家供大家参考,具体如下: 一、php页面 $score = 0; if(!empty($_GET['value'])){ //接收的值...

php下尝试使用GraphicsMagick的缩略图功能

php下尝试使用GraphicsMagick的缩略图功能

常用的图片处理工具有GD,ImageMagick,GraphicsMagick等等。GD就是个阿斗,略过不提;ImageMagick是目前最流行的图片处理工具,它的功能非常丰富;Grap...

php面向对象程序设计中self与static的区别分析

本文实例讲述了php面向对象程序设计中self与static的区别。分享给大家供大家参考,具体如下: 1、假设我们有个Car类,它有2个方法:model()和getModel()。...