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 class human{ public static $le...

PHP的5个安全措施小结

开发人员、数据库架构师和系统管理员在部署PHP应用程序到服务器之前都应该采取预防措施。大部分预防措施可以通过几行代码或者把应用程序设置稍作调整即可完成。   #1:管理安装脚本   如果...

PHP框架自动加载类文件原理详解

描述:公司项目PHP用作中间转发层(接收http请求,用 socket跟c++做通信),由于代码没有用到框架,这些东西自然就是之前的人自己写的。最近需要对这个底层进行优化,于是便看了下这...

采用memcache在web集群中实现session的同步会话

使用memcache来同步session是还是不错的,当然也可以通过redis来保存session,可以php开启并将Session存储到Redis缓存,下面是设置利用memcache在...

php使用Jpgraph绘制3D饼状图的方法

php使用Jpgraph绘制3D饼状图的方法

本文实例讲述了php使用Jpgraph绘制3D饼状图的方法。分享给大家供大家参考。具体实现方法如下: <?php include ("src/jpgraph.php"...