dqusbxh44823 2014-02-24 06: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 06: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部