0

ページに .html ファイルを直接書き込んで作成し、それをユーザーが自分のローカル マシンにダウンロードできるようにする必要があります。.html ファイルは、データベース要求からの完全な OS 情報になります。次のようになります。

<?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>
4

2 に答える 2

3
header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");

これを修正して、適切な場所からファイル名を指定するようにします (未定義の変数からハードディスクに存在しないファイルの名前から計算しようとするのではなく)。

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

これを取り除きます。Content-Disposition が存在することを知らない人にとっては、汚いハックです。

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

コンテンツの長さを指定する場合は、変数のデータを使用して指定する必要があり、前述の存在しないファイルのファイル サイズを測定する必要はありません。

次に、あなたのエコー$content

于 2013-08-09T09:47:30.777 に答える
-1

最初に、コードを使用して作成するかどうかを確認するために、生成したいファイルはありますか?

が生成された場合は、ステップ 2 に進みます。

Else ステップ 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:

ファイルが元のものであることを確認した後、ファイルをダウンロードします

ファイルのダウンロード 方法 簡単な方法: 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>
于 2013-08-09T09:50:54.207 に答える