dqiaw48488 2010-06-23 20:29 采纳率: 0%
浏览 82

如何让php处理不完整文件的文件下载?

So for example, I have a file being uploaded through ftp and file size changes. I do know the file size beforehand. So how would I make php to send it to clients browsers even if file is not finished uploading and waiting for file size to match before it finish buffering. The code below won't update the handle and doesn't see changes made to file. I could pass fopen everytime but maybe there is a proper way? Thanks

$buffer = '';
$cnt =0;
$handle = fopen($filename, 'rb');
if ($handle === false) {
  return false;
}
while ($cnt < $totalSize) {
  $buffer = fread($handle, 2048);
  echo $buffer;
  ob_flush();
  flush();
  $bufferLen = strlen($buffer);
  $cnt += $bufferLen;
  if(!$bufferLen) sleep(1);
}
fclose($handle);
  • 写回答

1条回答 默认 最新

  • doufunuo4787 2010-06-23 20:38
    关注

    Will it help if you close and reopen your file? Then you could fseek to where you left off, and continue pushing the rest of the file.

    Try to add something like this after cnt += $bufferLen;

    if(feof($handle) && ($cnt < $totalSize)) {
      fclose($handle);
      sleep(1);
      $handle = fopen($filename, 'rb');
      fseek($handle, $cnt);
    }
    

    Remove the if(!bufferLen) sleep(1); statement.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题