翼度科技»论坛 编程开发 PHP 查看内容

PHP实现文件下载限速功能的方法详解

8

主题

8

帖子

24

积分

新手上路

Rank: 1

积分
24
限速下载文件的原理是通过控制数据传输的速率来限制下载的速度。在PHP中,我们可以通过以下步骤来实现限速下载文件的功能:
设置下载响应头: 在发送文件内容之前,设置正确的HTTP响应头,包括Content-Type、Content-Disposition等,以便浏览器能够正确处理文件下载。
打开文件并读取内容: 使用PHP的文件操作函数,打开要下载的文件并读取其中的内容。在读取文件内容时,我们需要进行限速处理,确保下载速率不超过预设的限制。
控制下载速率: 在循环读取文件内容的过程中,通过控制每次读取的数据量和每次读取的时间间隔来实现限速。通常是通过 usleep() 函数来实现暂停一段时间。
输出文件内容: 将读取的文件内容输出到浏览器,实现文件的下载。通过循环读取文件内容并输出,直到文件的所有内容都被发送给浏览器。
关闭文件句柄: 在下载完成后,关闭文件句柄,释放资源。
  1. /**
  2. * 下载文件并限速
  3. *
  4. * @param string $file_path 文件路径
  5. * @param int $kilobytes 每秒下载的 KB 数
  6. */
  7. function downloadFileWithSpeedLimit($file_path, $kilobytes = 100) {
  8.     if (file_exists($file_path)) {
  9.         // 获取文件大小
  10.         $file_size = filesize($file_path);

  11.         // 设置下载响应头
  12.         header('Content-Description: File Transfer');
  13.         header('Content-Type: application/octet-stream');
  14.         header('Content-Disposition: attachment; filename=' . basename($file_path));
  15.         header('Content-Transfer-Encoding: binary');
  16.         header('Expires: 0');
  17.         header('Cache-Control: must-revalidate');
  18.         header('Pragma: public');
  19.         header('Content-Length: ' . $file_size);

  20.         // 打开文件并进行读取
  21.         $file = fopen($file_path, "rb");

  22.         // 设置下载速度限制
  23.         $limit_speed = $kilobytes * 1024; // 转换为字节
  24.         $start_time = microtime(true);
  25.         while (!feof($file)) {
  26.             echo fread($file, $limit_speed);
  27.             flush();
  28.             usleep(1000000 / $limit_speed);
  29.             $elapsed_time = microtime(true) - $start_time;
  30.             if ($elapsed_time > 1) {
  31.                 $start_time = microtime(true);
  32.             }
  33.         }

  34.         // 关闭文件句柄
  35.         fclose($file);
  36.         exit;
  37.     } else {
  38.         echo "文件不存在!";
  39.     }
  40. }

  41. // 调用方法,下载文件并限速
  42. $file_path = "your_file_path"; // 替换为要下载的文件路径
  43. downloadFileWithSpeedLimit($file_path, 100); // 设置下载速率为每秒 100KB
复制代码
方法补充
除了上文的方法,小编还为大家整理了其他PHP实现文件下载限速的方法,需要的可以参考下
大文件限速下载
  1. <?php
  2. //设置文件最长执行时间
  3. set_time_limit(0);
  4. if (isset($_GET['filename']) && !empty($_GET['filename'])) {
  5.   $file_name = $_GET['filename'];
  6.   $file = __DIR__ . '/assets/' . $file_name;
  7. } else {
  8.   echo 'what are your searching for?';
  9.   exit();
  10. }
  11. if (file_exists($file) && is_file($file)) {
  12.   $filesize = filesize($file);
  13.   header('Content-Description: File Transfer');
  14.   header('Content-Type: application/octet-stream');
  15.   header('Content-Transfer-Encoding: binary');
  16.   header('Accept-Ranges: bytes');
  17.   header('Expires: 0');
  18.   header('Cache-Control: must-revalidate');
  19.   header('Pragma: public');
  20.   header('Content-Length: ' . $filesize);
  21.   header('Content-Disposition: attachment; filename=' . $file_name);
  22.   // 打开文件
  23.   $fp = fopen($file, 'rb');
  24.   // 设置指针位置
  25.   fseek($fp, 0);
  26.   // 开启缓冲区
  27.   ob_start();
  28.   // 分段读取文件
  29.   while (!feof($fp)) {
  30.     $chunk_size = 1024 * 1024 * 2; // 2MB
  31.     echo fread($fp, $chunk_size);
  32.     ob_flush(); // 刷新PHP缓冲区到Web服务器    flush(); // 刷新Web服务器缓冲区到浏览器
  33.     sleep(1); // 每1秒 下载 2 MB
  34.   }
  35.   // 关闭缓冲区
  36.   ob_end_clean();
  37.   fclose($fp);
  38. } else {
  39.   echo 'file not exists or has been removed!';
  40. }
  41. exit();
复制代码
php控制文件下载速度的方法
  1. <?php
  2. /*
  3. * set here a limit of downloading rate (e.g. 10.20 Kb/s)
  4. */
  5. $download_rate = 10.20;
  6. $download_file = 'download-file.zip';
  7. $target_file = 'target-file.zip';
  8. if(file_exists($download_file)){
  9.   /* headers */
  10.   header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
  11.   header('Cache-control: private');
  12.   header('Content-Type: application/octet-stream');
  13.   header('Content-Length: '.filesize($download_file));
  14.   header('Content-Disposition: filename='.$target_file);
  15.   /* flush content */
  16.   flush();
  17.   /* open file */
  18.   $fh = @fopen($download_file, 'r');
  19.   while(!feof($fh)){
  20.    /* send only current part of the file to browser */
  21.    print fread($fh, round($download_rate * 1024));
  22.    /* flush the content to the browser */
  23.    flush();
  24.    /* sleep for 1 sec */
  25.    sleep(1);
  26.   }
  27.   /* close file */
  28.   @fclose($fh);
  29. }else{
  30.   die('Fatal error: the '.$download_file.' file does not exist!');
  31. }
  32. ?>
复制代码
php限制下载速度
  1. // local file that should be send to the client
  2. $local_file = 'test-file.zip';
  3. // filename that the user gets as default
  4. $download_file = 'your-download-name.zip';
  5. // set the download rate limit (=> 20,5 kb/s)
  6. $download_rate = 20.5;
  7. if(file_exists($local_file) && is_file($local_file)) {
  8. // send headers
  9. header('Cache-control: private');
  10. header('Content-Type: application/octet-stream');
  11. header('Content-Length: '.filesize($local_file));
  12. header('Content-Disposition: filename='.$download_file);
  13. // flush content
  14. flush();
  15. // open file stream
  16. $file = fopen($local_file, "r");
  17. while (!feof($file)) {
  18. // send the current file part to the browser
  19. print fread($file, round($download_rate * 1024));
  20. // flush the content to the browser
  21. flush();
  22. // sleep one second
  23. sleep(1);
  24. }
  25. // close file stream
  26. fclose($file);
  27. }
  28. else {
  29. die('Error: The file '.$local_file.' does not exist!');
  30. }
复制代码
到此这篇关于PHP实现文件下载限速功能的方法详解的文章就介绍到这了,更多相关PHP文件下载限速内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:https://www.jb51.net/program/3162208ju.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具