dqusbxh44823 2014-02-24 14:23
浏览 68
已采纳

二进制安全函数,用于在PHP中发送文件块

I've crafted my own version of simple solution, for sending large files in PHP. It uses file chunks. This solution is proted from various sources and I copy-pasted following code to implement "chunking":

$handle = fopen($filename, 'rb'); 

while (!feof($handle))
{ 
    print(@fread($handle, $chunksize));

    ob_flush();
    flush();
} 

fclose($handle);

Since it uses print to send each chunk to the browser, it is very sensitive to character encoding, in which PHP script is encoded. For example, I noticed, that when script is saved in ANSI, downloaded files are corrupted. Only, if I save & upload script encoded in utf-8, file are fine.

Is there a better function, than print to do the same (send piece of file to browser), that would be independend of script file encoding -- because, for example, would force binary transfer to browser?

  • 写回答

1条回答 默认 最新

  • duanrou5680 2014-02-24 14:50
    关注

    For small files you should use readfile, that makes all for you to send a file to the client. For really big files these function probably don't work due to memory problems.

    Also there is a function file_get_contents to get all file content into a variable if you require to process it before send. Like readfile is recommended only for "small" files. "small" depends on the memory allowed to run a PHP process (memory_limit parameter in php.ini). Usually servers set it from 8M to 128M and by default is 16MB.

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

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state