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是不是打开了gd库。复制代码 代码如下:<?phpif(extension_loaded('gd')) {  echo '你可以使用gd<...

基于PHP编程注意事项的小结

 1、php隐性的三元操作符(?:)优先级问题: 例1:复制代码 代码如下:    $person = $who or $person = "la...

PHP实现数字补零功能的2个函数介绍

在PHP中至少有两个函数能够帮助我们快速实现数字补零:首先是PHP str_pad函数:复制代码 代码如下:#str_pad — 使用另一个字符串填充字符串为指定长度顾名思义这个函数是针...

PHP 模拟登陆功能实例详解

PHP 模拟登陆功能实例详解

本文实例讲述了PHP 模拟登陆功能。分享给大家供大家参考,具体如下: 说明:该模拟登陆实例仅适用于没有验证码的模拟登陆实例 该程序基本功能是,模拟登陆高校图书馆网站,并获取读者信息和借书...

thinkphp使用phpmailer发送邮件的方法

本文实例讲述了thinkphp使用phpmailer发送邮件的方法。分享给大家供大家参考。具体分析如下: phpmailer发送邮件是php开发者首选的一个邮件发送插件了,下面我来介绍怎...