views:

19

answers:

1

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);
A: 

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.

Ivar Bonsaksen
thats right, thanks
Lasiaf