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自动加载机制。分享给大家供大家参考,具体如下: 在php中,我们一般使用 require, requre_once, include, include_once 这四...

PHP 调试工具Debug Tools

PHP 调试工具Debug Tools

发现了个调试PHP源码的好同志: PHP DEBUG TOOLS 其项目地址: http://freshmeat.net/projects/php-debug-tools/ 文件下载地址...

php计算一个文件大小的方法

本文实例讲述了php计算一个文件大小的方法。分享给大家供大家参考。具体如下: <?php function dirSize($directoty){ $di...

php基于socket实现SMTP发送邮件的方法

本文实例讲述了php基于socket实现SMTP发送邮件的方法。分享给大家供大家参考。具体分析如下: php采用socket通过SMTP发送邮件。 用的是php的php-sockets扩...

PHP简洁函数小结

PHP简洁函数 主题:类菌体PHP简洁函数 简述:PHP简单明了函数语法 适合人群:对开源社区感兴趣,对php感兴趣,有一点时间了解下php 备注:希望大家抛砖,仍蛋,呵呵 1、与mys...