<legend id='sHOro'><style id='sHOro'><dir id='sHOro'><q id='sHOro'></q></dir></style></legend>

        <bdo id='sHOro'></bdo><ul id='sHOro'></ul>

    1. <small id='sHOro'></small><noframes id='sHOro'>

    2. <tfoot id='sHOro'></tfoot>
      <i id='sHOro'><tr id='sHOro'><dt id='sHOro'><q id='sHOro'><span id='sHOro'><b id='sHOro'><form id='sHOro'><ins id='sHOro'></ins><ul id='sHOro'></ul><sub id='sHOro'></sub></form><legend id='sHOro'></legend><bdo id='sHOro'><pre id='sHOro'><center id='sHOro'></center></pre></bdo></b><th id='sHOro'></th></span></q></dt></tr></i><div id='sHOro'><tfoot id='sHOro'></tfoot><dl id='sHOro'><fieldset id='sHOro'></fieldset></dl></div>
    3. PHP 强制下载导致 0 字节文件

      PHP Force Download Causing 0 Byte Files(PHP 强制下载导致 0 字节文件)

        <tfoot id='iaL3J'></tfoot>

            <tbody id='iaL3J'></tbody>
          <i id='iaL3J'><tr id='iaL3J'><dt id='iaL3J'><q id='iaL3J'><span id='iaL3J'><b id='iaL3J'><form id='iaL3J'><ins id='iaL3J'></ins><ul id='iaL3J'></ul><sub id='iaL3J'></sub></form><legend id='iaL3J'></legend><bdo id='iaL3J'><pre id='iaL3J'><center id='iaL3J'></center></pre></bdo></b><th id='iaL3J'></th></span></q></dt></tr></i><div id='iaL3J'><tfoot id='iaL3J'></tfoot><dl id='iaL3J'><fieldset id='iaL3J'></fieldset></dl></div>
          • <bdo id='iaL3J'></bdo><ul id='iaL3J'></ul>
            <legend id='iaL3J'><style id='iaL3J'><dir id='iaL3J'><q id='iaL3J'></q></dir></style></legend>

                <small id='iaL3J'></small><noframes id='iaL3J'>

              • 本文介绍了PHP 强制下载导致 0 字节文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试使用 PHP 从我的 Web 服务器强制下载文件.我不是 PHP 专家,但我似乎无法解决以 0 字节大小下载文件的问题.

                I'm trying to force download files from my web server using PHP. I'm not a pro in PHP but I just can't seem to get around the problem of files downloading in 0 bytes in size.

                代码:

                $filename = "FILENAME...";
                
                header("Content-type: $type");
                header("Content-Disposition: attachment;filename=$filename");
                header("Content-Transfer-Encoding: binary");
                header('Pragma: no-cache');
                header('Expires: 0');
                set_time_limit(0);
                readfile($file);
                

                有人可以帮忙吗?谢谢.

                Can anybody help? Thanks.

                推荐答案

                您没有检查文件是否存在.尝试使用这个:

                You're not checking that the file exists. Try using this:

                $file = 'monkey.gif';
                
                if (file_exists($file))
                {
                    header('Content-Description: File Transfer');
                    header('Content-Type: application/octet-stream');
                    header('Content-Disposition: attachment; filename='.basename($file));
                    header('Content-Transfer-Encoding: binary');
                    header('Expires: 0');
                    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                    header('Pragma: public');
                    header('Content-Length: ' . filesize($file));
                    ob_clean();
                    flush();
                    readfile($file);
                    exit;
                }else
                {
                    echo "File does not exists";
                }
                

                看看你得到了什么.

                您还应该注意,这会强制下载为八位字节流,即纯二进制文件.一些浏览器很难理解文件的确切类型.例如,如果您发送带有 Content-Type: application/octet-stream 标头的 GIF,则浏览器可能不会将其视为 GIF 图像.您应该添加特定检查以确定文件的内容类型,并发送适当的 Content-Type 标头.

                You should also note that this forces a download as an octet stream, a plain binary file. Some browsers will struggle to understand the exact type of the file. If, for example, you send a GIF with a header of Content-Type: application/octet-stream, then the browser may not treat it like a GIF image. You should add in specific checks to determine what the content type of the file is, and send an appropriate Content-Type header.

                这篇关于PHP 强制下载导致 0 字节文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Is PHP or PHP based web framework stateful or stateless?(PHP 或基于 PHP 的 Web 框架是有状态的还是无状态的?)
                How to parse django style template tags(如何解析 django 样式模板标签)
                What is a good setup for editing PHP in Emacs?(在 Emacs 中编辑 PHP 的好设置是什么?)
                How to check whether specified PID is currently running without invoking ps from PHP?(如何在不从 PHP 调用 ps 的情况下检查指定的 PID 当前是否正在运行?)
                What#39;s the difference between escapeshellarg and escapeshellcmd?(escapeshellarg 和escapeshellcmd 有什么区别?)
                php in background exec() function(php 后台 exec() 函数)

                  <tfoot id='UCeTL'></tfoot>
                      <legend id='UCeTL'><style id='UCeTL'><dir id='UCeTL'><q id='UCeTL'></q></dir></style></legend>
                        <tbody id='UCeTL'></tbody>
                      <i id='UCeTL'><tr id='UCeTL'><dt id='UCeTL'><q id='UCeTL'><span id='UCeTL'><b id='UCeTL'><form id='UCeTL'><ins id='UCeTL'></ins><ul id='UCeTL'></ul><sub id='UCeTL'></sub></form><legend id='UCeTL'></legend><bdo id='UCeTL'><pre id='UCeTL'><center id='UCeTL'></center></pre></bdo></b><th id='UCeTL'></th></span></q></dt></tr></i><div id='UCeTL'><tfoot id='UCeTL'></tfoot><dl id='UCeTL'><fieldset id='UCeTL'></fieldset></dl></div>
                        <bdo id='UCeTL'></bdo><ul id='UCeTL'></ul>

                        <small id='UCeTL'></small><noframes id='UCeTL'>