当前位置:Gxlcms > PHP教程 > php如何下载远程大文件以及获取文件大小的实例代码分享

php如何下载远程大文件以及获取文件大小的实例代码分享

时间:2021-07-01 10:21:17 帮助过:3人阅读

下面小编就为大家带来一篇php下载远程大文件(获取远程文件大小)的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

废话不多说,直接上代码

  1. <?php
  2. // 暂不支持断点续传
  3. // $url = 'http://www.mytest.com/debian.iso'; 不知道为何获取本地文件大小为0
  4. $url = 'http://192.168.8.93/download/vm-672/18/0.vmdk';
  5. $file = basename($url);
  6. $header = get_headers($url, 1);
  7. $size = $header['Content-Length'];
  8. $fp = fopen($url, 'rb');
  9. if ($fp === false) exit('文件不存在或打开失败');
  10. header('Content-Description: File Transfer');
  11. header('Content-Type: application/octet-stream');
  12. header('Content-Disposition: attachment; filename="'.$file.'"');
  13. header('Content-Transfer-Encoding: binary');
  14. header('Expires: 0');
  15. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  16. header('Pragma: public');
  17. header('Content-Length: ' . $size);
  18. ob_clean();
  19. ob_end_flush();
  20. set_time_limit(0);
  21. $chunkSize = 1024 * 1024;
  22. while (!feof($fp)) {
  23. $buffer = fread($fp, $chunkSize);
  24. echo $buffer;
  25. ob_flush();
  26. flush();
  27. }
  28. fclose($fp);
  29. exit;

以上就是php如何下载远程大文件以及获取文件大小的实例代码分享的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行