Home  >  Article  >  Backend Development  >  PHP method to download files using file streams (attached: solving the problem of garbled downloaded file content), _PHP tutorial

PHP method to download files using file streams (attached: solving the problem of garbled downloaded file content), _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:21:28902browse

PHP uses the file stream method to download files (attached: solving the problem of garbled downloaded file content),

I remember doing a game private server in high school, and the game homepage at that time was written in PHP Yes, because the files are very fixed, the client, login and some small tools, the number of files is not very large, so the download links are written directly and directly linked to the file directory of the local server. Today, a friend in the QQ group suddenly I was asked to use file stream to implement PHP download function, and I wrote a small demo. The code is very simple and the comments are complete. Just go to the code~

1. flush — Refresh the output buffer

2. ob_clean — Clear (erase) the output buffer

This function is used to discard the contents of the output buffer.

This function will not destroy the output buffer, but functions like ob_end_clean() will destroy the output buffer.

Note: The above two functions can solve the problem of garbled content of PHP downloaded files

<?<span>php

    </span><span>//</span><span>fname为要下载的文件名
    //$fpath为下载文件所在文件夹,默认是downlod</span>
    <span>function</span> download(<span>$fname</span>,<span>$fpath</span>="download/"<span>){

        </span><span>//</span><span>避免中文文件名出现检测不到文件名的情况,进行转码utf-8->gbk</span>
        <span>$filename</span>=<span>iconv</span>('utf-8', 'gb2312', <span>$fname</span><span>);
        </span><span>$path</span>=<span>$fpath</span>.<span>$filename</span><span>;
        </span><span>if</span>(!<span>file_exists</span>(<span>$path</span>)){<span>//</span><span>检测文件是否存在</span>
            <span>echo</span> "文件不存在!"<span>;
            </span><span>die</span><span>();
        }

        </span><span>$fp</span>=<span>fopen</span>(<span>$path</span>,'r');<span>//</span><span>只读方式打开</span>
        <span>$filesize</span>=<span>filesize</span>(<span>$path</span>);<span>//</span><span>文件大小

        //返回的文件(流形式)</span>
        <span>header</span>("Content-type: application/octet-stream"<span>);
        </span><span>//</span><span>按照字节大小返回</span>
        <span>header</span>("Accept-Ranges: bytes"<span>);
        </span><span>//</span><span>返回文件大小</span>
        <span>header</span>("Accept-Length: <span>$filesize</span>"<span>);
        </span><span>//</span><span>这里客户端的弹出对话框,对应的文件名</span>
        <span>header</span>("Content-Disposition: attachment; filename=".<span>$filename</span><span>);
        </span><span>//</span><span>================重点====================</span>
        <span>ob_clean</span><span>();
        </span><span>flush</span><span>();
        </span><span>//</span><span>=================重点===================
        //设置分流</span>
        <span>$buffer</span>=1024<span>;
        </span><span>//</span><span>来个文件字节计数器</span>
        <span>$count</span>=0<span>;
        </span><span>while</span>(!<span>feof</span>(<span>$fp</span>)&&(<span>$filesize</span>-<span>$count</span>>0<span>)){
            </span><span>$data</span>=<span>fread</span>(<span>$fp</span>,<span>$buffer</span><span>);
            </span><span>$count</span>+=<span>$data</span>;<span>//</span><span>计数</span>
            <span>echo</span> <span>$data</span>;<span>//</span><span>传数据给浏览器端</span>
<span>        }

        </span><span>fclose</span>(<span>$fp</span><span>);

    }


    download(</span>"CGEX脚本清单.doc"<span>);



</span>?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/858176.htmlTechArticlePHP uses file stream to download files (attached: solving the problem of garbled downloaded file content), I remember playing games in high school Private server, the game homepage at that time was written in PHP, because the files were very...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn