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中soap的用法实例

本文实例讲述了PHP中soap的用法,分享给大家供大家参考。具体用法分析如下: PHP 使用soap有两种方式。 一、用wsdl文件 服务器端: 复制代码 代码如下:<?p...

php array_merge下进行数组合并的代码

如  $array3=array("性别"=>"男","姓名"=>"不是女人");  $array4=array("性别"=>"不知道","长相"=...

php下把数组保存为文件格式的实例应用

我使用过两种办法: 第一种是数组序列化,简单,但是调用时比较麻烦一些;第二种是保存为标准的数组格式,保存时麻烦但是调用时简单。 第一种方法: PHP代码 复制代码 代码如下: $file...

PHP目录函数实现创建、读取目录教程实例

今天主要介绍在PHP网站开发中文件目录函数的应用。在PHP网站开发中,我们时常需要读取目录文件信息或者创建目录以存放必要的文件,而当目录文件大小超出规定大小时我们又需要删除目录文件,如手...

php自动加载机制的深入分析

php自动加载机制的深入分析

一、php中实现自动加载的方法1.使用require,include,require_once,include_once手工进行加载。2.使用__autoload来进行自动加载3.使用s...