如何让 php 处理不完整文件的文件下载?
例如,我有一个通过 ftp 上传的文件,并且文件大小发生了变化。我事先知道文件大小。那么,即使文件尚未完成上传并等待文件大小在完成缓冲之前匹配,我如何使 php 将其发送到客户端浏览器。下面的代码不会更新句柄,也不会看到对文件所做的更改。我每次都可以通过 fopen 但也许有正确的方法?谢谢
$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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您关闭并重新打开文件会有帮助吗?然后您可以 fseek 到您上次停下的位置,并继续推送文件的其余部分。
尝试在 cnt += $bufferLen; 之后添加类似的内容
删除
if(!bufferLen) sleep(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;
Remove the
if(!bufferLen) sleep(1);
statement.