如何让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个回答

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.

doulu1020
doulu1020 好的,谢谢
10 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐