【问题标题】:Write and create .html file download编写和创建 .html 文件下载
【发布时间】:2013-08-11 04:48:38
【问题描述】:

我需要直接在页面上编写和创建一个 .html 文件,然后让用户将其下载到他的本地计算机上。 .html 文件将包含来自数据库请求的完整操作系统信息。 它看起来像这样:

<?php
    $content = "<!DOCTYPE html><html lang="en"><head></head><body>";
    $content .= "<div>Hello World</div>";
    $content .= get_all_contents_from_db();
    $content .= "</body></html>";
?>

我看到了一个代码,可让您将所看到的页面下载到包含此代码的文件中:

    <?php 
    if(isset($_POST['download']))
    {
        header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
        header("Content-Type: application/force-download");
        header("Content-Length: " . filesize($File));
        header("Connection: close");
    }
    ?>
So the question is: How do I create a downloadable html file using php?

**PART 2**
<form method="POST" action=''>
    <button name="download">Download File</button>
</form>
<?php 

    $content = "<!DOCTYPE html><html lang=\"en\"><head></head><body>";
    $content .= "<div>Hello World</div>";
    $content .= "</body></html>";

    if(isset($_POST['download'])){

        echo $content;
        header("Content-Disposition: attachment; filename=\"slideshow.html\"");
        header("Content-Length: " . filesize($content));
        echo $content;
    }
?>

输出

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
    </head>
    <body>
    <div class="main">Dashboard</br>
        <form method="POST" action=''>
            <button name="download">Download File</button>
        </form>
        <!DOCTYPE html><html lang="en"><head></head><body><div>Hello World</div></body></html><!DOCTYPE html><html lang="en"><head></head><body><div>Hello World</div></body></html>            </div>
    </div>
    </body>
</html>

【问题讨论】:

  • 你的问题缺少问题。
  • 另外,application/force-download 是不存在的内容类型。
  • HTML 文档的 Content-type 是 text/html
  • application/force-download 可能是不存在的内容类型,但已被用作黑客;参见例如stackoverflow.com/a/10616753/469210
  • 您不需要使用 hack。这就是 Content-Disposition 的用途。

标签: php download


【解决方案1】:
header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");

修复此问题,以便您从某个合理的位置指定文件名(而不是尝试根据未定义变量的硬盘上不存在的文件名来计算它)。

header("Content-Type: application/force-download");

摆脱这个。对于不知道 Content-Disposition 存在的人来说,这是一个肮脏的黑客攻击。

header("Content-Length: " . filesize($File));

如果要指定内容长度,则需要使用变量中的数据来执行此操作,而不是测量上述不存在文件的文件大小。

然后回显您的$content

【讨论】:

  • 嗨昆汀,谢谢你的回答!我已经设法从 $content 下载了包含内容的文件,但是当我在我的文本编辑器中打开文件时,它带有来自它的原始文件的额外标签和标题。如何仅使用 var $content 中包含的文本下载文件?我已经编辑了这个问题,所以你可以看到我到目前为止的内容。
  • 你需要不要输出你不想要的东西。该脚本应提供 HTML 文件以供下载,仅此而已。您不能同时返回要显示的页面和要下载的文件
  • 有没有更好的方法来删除我不想要的代码行,而不是使用 str_replace ('all the lines to remove', '', $content)?
  • 是的。不要一开始就添加这些行。要么将你的脚本一分为二,要么使用 if/else。
  • 要使用字符串并在需要的地方拆分,我如何在下载文件之前保存所有内容。这样的事情可能有助于理解我的问题: $all = header("Content-Disposition: attachment; filename=\"slideshow.html\"");
【解决方案2】:

首先尝试确定您想要生成的文件是否要通过您的代码生成?

如果已生成则转到第 2 步

其他步骤 1 在第一个..中添加错误报告功能。

          <?php
       error_reporting(E_ALL ^ E_NOTICE);
         $content = "<!DOCTYPE html><html lang="en"><head></head><body>";
         $content .= "<div>Hello World</div>";
         $content .= get_all_contents_from_db();
         $content .= "</body></html>";?>

因为在我的情况下(当我需要他们通过 php 生成新文件时)由于未生成文件而添加未定义错误这将在错误报告功能的帮助下解决..

第 2 步:

确保您的文件是原始文件后,然后下载文件

文件下载方式 简单的方法: 尝试 a href 标记以简单地链接到您要下载的文件

            <?php
           <a href="location of sample.html file">Sample File</a>
            ?>

通过编码方式: (假设你要下载changepwd文件那么它的语法就是

       <?php
      //$file="chngpwd.php";
     if($_REQUEST['download'])
       {
 $file="chngpwd.php";
 $fs = filesize($file);
$ft = filetype($file);

header('Content-Type:application/'.$ft);
header('Content-Disposition:attachment;filename='.$file);
header('Content-length'.$fs);
echo file_get_contents($file);
      }?>

      <form method="post" action="" enctype="multipart/form-data">
 <input type="submit" name="download" value="DOWNLOAD">

     </form>

【讨论】:

    猜你喜欢
    • 2019-02-23
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 2011-12-19
    • 2015-09-16
    • 2012-10-05
    相关资源
    最近更新 更多