首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP不生成内容

PHP不生成内容
EN

Stack Overflow用户
提问于 2017-03-21 07:57:00
回答 2查看 49关注 0票数 1

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

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

表格很简单:

代码语言:javascript
复制
<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文件:

代码语言:javascript
复制
<?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(); 
?>

你知道吗,问题在哪里?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-21 08:01:06

尝尝这个

在html_form_submit.php文件:

代码语言:javascript
复制
<?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(); 
?>
票数 1
EN

Stack Overflow用户

发布于 2017-03-21 08:01:46

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

代码语言:javascript
复制
<?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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42921202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档