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 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题