【问题标题】:create, write and download a txt file using php使用 php 创建、编写和下载 txt 文件
【发布时间】:2019-02-23 21:43:12
【问题描述】:
$file = fopen("test.txt", "w") or die("Unable to open file!");
fwrite($file, "lorem ipsum");
fclose($file);

header("Content-Disposition: attachment; filename=\"" . basename($file) . "\"");
header("Content-Type: application/force-download");
header("Content-Length: " . filesize($file));
header("Connection: close");

文件已创建/打开并写入,但未下载。

有什么帮助吗?

【问题讨论】:

  • 输出nothing怎么下载?
  • 在磁盘上创建文件和将文件发送到客户端是完全不同的两件事。您正在创建一个文件,然后什么都不发送给客户端。

标签: php


【解决方案1】:

我认为您只需要下载过程的内容

<?PHP

//config
$namefile = "test.txt";
$content = "lorem ipsum";

//save file
$file = fopen($namefile, "w") or die("Unable to open file!");
fwrite($file, $content);
fclose($file);

//header download
header("Content-Disposition: attachment; filename=\"" . $namefile . "\"");
header("Content-Type: application/force-download");
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header("Content-Type: text/plain");

echo $content;

谢谢。

【讨论】:

    【解决方案2】:

    创建文件后,您应该在使用文件处理程序时将其内容输出到浏览器,例如fpassthru

    $path = "test.txt";
    $file = fopen($path, "w") or die("Unable to open file!");
    fwrite($file, "lorem ipsum");
    //fclose($file); // do not close the file
    rewind($file);   // reset file pointer so as output file from the beginning
    
    // `basename` and `filesize` accept path to file, not file descriptor
    header("Content-Disposition: attachment; filename=\"" . basename($path) . "\"");
    header("Content-Type: application/force-download");
    header("Content-Length: " . filesize($path));
    header("Connection: close");
    fpassthru($file);
    exit();
    

    【讨论】:

      【解决方案3】:

      正确的做法是:

      <?php
      
      $file = "test.txt";
      $txt = fopen($file, "w") or die("Unable to open file!");
      fwrite($txt, "lorem ipsum");
      fclose($txt);
      
      header('Content-Description: File Transfer');
      header('Content-Disposition: attachment; filename='.basename($file));
      header('Expires: 0');
      header('Cache-Control: must-revalidate');
      header('Pragma: public');
      header('Content-Length: ' . filesize($file));
      header("Content-Type: text/plain");
      readfile($file);
      
      ?>
      

      【讨论】:

      • @puerto 验证您是否正确复制了它。我永远不会发布我没有先测试自己的答案。它工作得很好:)
      • @puerto 附带说明:请记住,您不能在标头之前有输出的其他代码中使用它。并确保您的编辑器不会使用 BOM 保存代码
      • 我创建了一个新文件,其中只有您的代码并且它可以工作。单击一个按钮并通过 ajax 触发它 - 不起作用。那么我应该有一个专门的 php 页面供下载吗?
      • @puerto 是的。 Ajax 不能用于文件下载。您可以为下载页面制作一个弹出窗口。下载开始后会自动关闭。
      【解决方案4】:

      您需要输出一些内容才能让用户下载文件。 试试这个代码:

      $file = fopen("test.txt", "w") or die("Unable to open file!");
      fwrite($file, "lorem ipsum");
      fclose($file);
      
      header('Content-Description: File Transfer');
      header('Content-Type: application/octet-stream');
      header('Content-Disposition: attachment; filename="FILENAME"');
      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: test.txt');
      ob_clean();
      flush();
      readfile('text.txt');
      exit();
      

      FILENAME更改为您希望用户下载的文件的名称。

      【讨论】:

        猜你喜欢
        • 2013-03-28
        • 2013-08-11
        • 2012-10-05
        • 2012-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-16
        相关资源
        最近更新 更多