douchen7324 2014-11-14 23:13
浏览 52
已采纳

将文件保存为套接字并等到另一个进程写入结束

Let's say that we have a program X that continuously write a file for example the /tmp/test.file

I have a script in PHP that serves this file to the client so that he can download/read it.

How i'm able to do this continuously as the data are being written in the /tmp/test.file?

If i use the below code

<?php

$fp = fopen("/tmp/test.file","r");
while(!feof($fp))
{
 echo fread($fp,4096);
}
fclose($fp);

It instantly stops because it reads the whole file until EOF so it doesnt care if another process is writing at this file.

Thank you

  • 写回答

1条回答 默认 最新

  • dongxianrang9269 2014-11-14 23:23
    关注

    As a suggestion, use a continues Ajax call (for example 5 seconds interval) to the PHP script file, and get the file content via offset. You can store the current offset of file into SESSION or COOKIE.

    You can get file offset by using PHP's ftell function.

    As PHP's official document defines ftell():

    ftell — Returns the current position of the file read/write pointer

    Also, stream_get_contents() function might be useful as PHP's official document defines it:

    stream_get_contents — Reads remainder of a stream into a string

    then, In each interval, append the response data to a HTML tag.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改