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程序设计有所帮助。

相关文章

PHP7.1安装yaf扩展的方法

把PHP命令加到系统 我的PHP安装目录是/usr/local/webserver/php,所以phpize是/usr/local/webserver/php/bin/phpize,但是...

php实现生成code128条形码的方法详解

php实现生成code128条形码的方法详解

本文实例讲述了php实现生成code128条形码的方法。分享给大家供大家参考,具体如下: 效果图: <?php class BarCode128 { const...

PHP常见数组排序方法小结

本文实例讲述了PHP常见数组排序方法。 一、数组操作的基本函数 数组的键名和值 array_values($arr); 获得数组的值 array_keys($arr); 获得数组的键名...

php 获取客户端的真实ip

复制代码 代码如下: function GetIP(){ if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_I...

PHPExcel读取EXCEL中的图片并保存到本地的方法

HPExcel是相当强大的 MS Office Excel 文档生成类库,当需要输出比较复杂格式数据的时候,PHPExcel 是个不错的选择。 经过认真研究API文档和查看官方文档,终于...