【问题标题】:PHP is not generating contentPHP 不生成内容
【发布时间】:2017-08-12 18:28:58
【问题描述】:

我想用 PHP 制作一个简单的 HTML 文件生成器,用户在其中输入一些数据,然后 .php 文件生成带有 HTML 代码的输出,包括来自用户的数据。

问题是我的生成器只生成了一个空白的 .html 文件。

表格很简单:

<form action="html_form_submit.php" method="post">
<textarea name="name" rows="2" cols="20"> </textarea>
<input type="submit" value="Submit"/></form>

还有html_form_submit.php 文件:

<?php
    ob_start();
    $name = @$_POST['name'];
?>
<html><body>
Name: <?php echo $name; ?><br>
</body></html>

<?php
    $output = ob_get_contents(); 
    $filename = 'test.html';
    !$handle = fopen($filename, 'w');
    fwrite($handle, $output);
    fclose($handle);
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Length: ". filesize("$filename").";");
    header("Content-Disposition: attachment; filename=$filename");
    header("Content-Type: application/octet-stream; ");
    header("Content-Transfer-Encoding: binary");
    readfile($filename);
    ob_end_clean(); 
?>

你知道问题出在哪里吗?

【问题讨论】:

    标签: php html generator


    【解决方案1】:

    试试这个

    在 html_form_submit.php 文件中:

    <?php
        ob_start();
    if(isset($_POST['name'])){
        $name = $_POST['name'];
    }
    ?>
    <html><body>
    Name: <?php echo $name; ?><br>
    </body></html>
    
    <?php
        $output = ob_get_contents(); 
        $filename = 'test.html';
        !$handle = fopen($filename, 'w');
        fwrite($handle, $output);
        fclose($handle);
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Length: ". filesize("$filename").";");
        header("Content-Disposition: attachment; filename=$filename");
        header("Content-Type: application/octet-stream; ");
        header("Content-Transfer-Encoding: binary");
        readfile($filename);
        ob_end_clean(); 
    ?>
    

    【讨论】:

    • 非常感谢您的回答。它现在正在工作。
    • 另一个快速的问题:当我生成内容时,输出文件有时不包含整个 HTML 代码。我在那里使用了一些内联 css 样式、图像等。
    • 可能是单引号或双引号未正确关闭的地方。 :)
    • 不,我找到了问题的根源。我只是增加了缓冲区的大小,所以现在一切正常。再次感谢您!
    【解决方案2】:

    您应该在 $output = ob_get_contents(); 之后立即移动 ob_end_clean 的调用。 ob_end_clean 清除您的缓冲区,因此响应返回为空。 试试这样:

    <?php
        ob_start();
        $name = @$_POST['name'];
    ?>
        <html><body>
        Name: <?php echo $name; ?><br>
        </body></html>
    
    <?php
        $output = ob_get_contents(); 
        ob_end_clean(); 
        $filename = 'test.html';
        !$handle = fopen($filename, 'w');
        fwrite($handle, $output);
        fclose($handle);
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Length: ". filesize("$filename").";");
        header("Content-Disposition: attachment; filename=$filename");
        header("Content-Type: application/octet-stream; ");
        header("Content-Transfer-Encoding: binary");
        readfile($filename);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 2014-08-20
      • 2013-01-17
      • 2015-12-06
      • 2014-01-22
      相关资源
      最近更新 更多