【问题标题】:adding header will not make a file download添加标题不会使文件下载
【发布时间】:2013-08-08 16:59:27
【问题描述】:

添加标题是使链接可下载的方法已经有很好的记录,但我一定做错了什么。我编写文件,然后生成一些指向它的 HTML 链接,但文件不会下载,只会出现在浏览器中。

<?

   //unique id
   $unique = time();

   $test_name = "HTML_for_test_" . $unique . ".txt";

   //I should put the file name I want the header attached to here (?)
   header("Content-disposition: attachment;filename=$test_name");

   $test_handler = fopen($test_name, 'w') or die("no");

   fwrite($test_handler, $test);
   fclose($test_handler);

?>

<a href="<?=$test_name">Download File</a>

【问题讨论】:

    标签: php


    【解决方案1】:

    你只是在回显一个 HTML 标记 - 你应该阅读文件内容,就像在 PHP Doc 上建议的那样:

    <?php
    $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');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
    ?>
    

    【讨论】:

    • $file 只是文件名,或者应该是处理程序。我不明白 file_exists 如何处理文本字符串。
    • 这不起作用。它只是在屏幕上打印文件。
    • @thomas 'text string' 只是您要检查它是否存在的文件的路径。在此示例中,您要求检查的文件是“monkey.gif”,它与运行此代码的程序位于同一目录中(没有路径只有文件名)
    【解决方案2】:

    经过多次测试,这是我想出的组合,从那以后它在我所有的项目中都有效。

    处理下载请求的程序

     <?php
    
    // Do all neccessary security checks etc to make sure the user is allowed to download the file, etc..
    
    // 
    
     $file = '/path/to/your/storage/directory' . 'the_stored_filename';
     $filesize = filesize($file);
     header('Content-Description: File Transfer');
     header("Content-type: application/forcedownload");
     header("Content-disposition: attachment; filename=\"filename_to_display.example\"");
     header("Content-Transfer-Encoding: Binary");
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
     header('Pragma: public');
     header("Content-length: ".$filesize);
     ob_clean();
     flush();
     readfile("$file");
     exit;
    

    编辑

    上面的代码将进入它自己的文件,例如'download.php'。然后,您可以将其他页面上的下载链接更改为:

     <a href="download.php?filename=<?php echo $test_name; ?>">Download File</a>
    

    您还需要修改上面的 php 代码,使其适用于您的情况。在我刚刚给出的示例中,您可能想要更改这一行:

      $file = '/path/to/your/storage/directory' . 'the_stored_filename';
    

    到这里:

      $file = $_get['filename'];
    

    会在最基本的层面上工作。您可能希望在任何生产环境中盲目使用 $_get['filename'] 之前对其值进行清理。

    如果您想在用户请求下载的同一页面中显示下载内容,请查看我对这篇文章的回答:Dowloading multiple PDF files from javascript

    【讨论】:

    • 感谢抽奖。这对我不起作用。它将内容打印在屏幕上。如果我退出出口(如果我希望我的 HTML 呈现 &lt;a&gt;,我必须这样做),链接就像以前一样工作,只需将浏览器定向到文件。
    • 谢谢。 href 中的尖括号有什么作用?他们在正确的地方吗?他们似乎有点不对劲。我弄错了吗?
    • 为什么需要$_GET 来获取文件名?我可以说,$file = "thomas.txt" 吗?另外,文本文件可以获取标题吗?也许我应该将其设置为 HTML?但它不会打印为代码。
    • href 中放错位置的鳄鱼是我的错误 - 已修复。文件的名称从包含 href 的任何页面传递到 download.php 页面。您需要获取传入的文件的名称。不确定您在说什么:文本文件获取标题?
    猜你喜欢
    • 2022-01-12
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    • 2019-10-26
    相关资源
    最近更新 更多