douyue7536
2016-05-16 20:36
浏览 97
已采纳

echo + fread一个文件下载会导致什么开销?

If I have a php script that does nothing but the following...

$file = fopen($path, "r");
flock($file, LOCK_SH);

echo fread($file, filesize($path));

flock($file, LOCK_UN);
fclose($file);

...how much overhead is caused by accessing the script from a browser as opposed to simply accessing the actual file? Internally, is the entire file copied to some kind of buffer and then spit out again or is it nearly the same thing?

图片转代码服务由CSDN问答提供 功能建议

如果我有一个PHP脚本,除了以下内容之外什么都不做...... < pre> $ file = fopen($ path,“r”); nlock($ file,LOCK_SH); echo fread($ file,filesize($ path)); nflock($ file ,LOCK_UN); nclclose($ file);

...从浏览器访问脚本导致的开销是多少,而不是简单地访问实际 文件? 在内部,整个文件是复制到某种缓冲区然后再吐出来的还是几乎相同的东西?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongpin4611 2016-05-16 21:21
    已采纳

    Yes, it's read into a buffer. It's essentially equivalent to:

    $temp = fread($file, filesize($path));
    echo $temp;
    

    You can use fpassthru() to send to the client without reading everything into a buffer at once.

    fpassthru($file);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题