将生成的 HTML 代码保存到文件中



我创建了一个应用程序,该应用程序在Javascript中动态生成某些结构,并以结果形式将它们发送到PHP。生成器实际上已经完成,尽管我在将生成的 HTML 代码写入文件时遇到问题。我有这个PHP代码,我想将生成的HTML代码保存到文件中。如果我使用此代码:

$fp = fopen('plik.html', 'w');
fwrite($fp, file_get_contents('./generate.php'));
fclose($fp);

我得到这个文件内容:

error_reporting(E_ALL);
ini_set('display_errors',0); 
@$tytul = $_POST['k-title'];
@$opis = $_POST['k-desc'];
$fp = fopen('plik.html', 'w');
fwrite($fp, file_get_contents('./generate.php'));
fclose($fp);
// albo dla PHP 5:
file_put_contents('plik.html', file_get_contents('./generate.php'));
/* echo "<pre>";
print_r($_POST);
echo "</pre>"; */
if ($tytul == '' || $opis == '') {
echo '<div><p>Błąd</p><p>Uzupełnij wszystkie pola generatora!</p>';
} else {

echo '<div style="max-width: 1050px; margin-left: auto; margin-right: auto;">';
echo '<div style="padding-top: 50px; padding-bottom: 50px;">';
echo '<p style="font-size: 30px; text-align: center;">'.$tytul.'</p>';
echo '<p style="font-size: 15px; text-align: justify;">'.$opis.'</p>';
echo '</div>';
echo '<div style="width: 100%; text-align: center; background-color: #eee; font-weight: 200; line-height: 0.92em; padding: 23px 0; font-size: 30px;">Specyfikacja techniczna</div>';
echo '<table style="font-size: 14px; border: none; border-top: 1px solid #dbdbdb; border-bottom: 1px solid #dbdbdb; width: 100%;" border="0" cellspacing="0" cellpadding="5px"><tbody>';
$cechy = array_combine($_POST['cecha'], $_POST['cecha-opis']);
foreach($cechy as $klucz => $wartosc) {
echo '<tr><td style="padding: 3px 10px; text-align: right; border-right: 1px solid #dbdbdb;" width="50%">';
echo $klucz;
echo '</td>';
echo '<td style="padding: 3px 10px; text-align: left;" width="50%">';
echo $wartosc;
echo '</td>';
echo '</tr>';
}
echo '</tbody></table>';
$prezentacje = array();
for($i = 0; $i < count($_POST['pwidth']); $i++) {
$prezentacja = array();
$prezentacja['pwidth'] = $_POST['pwidth'][$i];
$prezentacja['pheight'] = $_POST['pheight'][$i];
$prezentacja['psource'] = $_POST['psource'][$i];
array_push($prezentacje, $prezentacja);
}
foreach($prezentacje as $p) {
echo '<img style="width: ' . $p['pwidth'] . 'px; height: ' . $p['pheight'] . 'px;" src="' . $p['psource'] . '" />';
}
echo '<div style="clear: both;"></div>';
$section = array();
for($a = 0; $a < count($_POST['sectionwidth']); $a++) {
$sesfull = array();
$sesfull['sectionwidth'] = $_POST['sectionwidth'][$a];
$sesfull['sectionheight'] = $_POST['sectionheight'][$a];
$sesfull['bg'] = $_POST['bg'][$a];
$sesfull['sectioncolor'] = $_POST['sectioncolor'][$a];
$sesfull['inputwidth'] = $_POST['inputwidth'][$a];
array_push($section, $sesfull);
}
foreach ($_POST['section'] as $sekcja) {
echo '<div style="margin: 0; float: left; width: '.$sekcja['sectionwidth'].'px; height: '.$sekcja['sectionheight'].'px; background: #'.$sekcja['bg'].'; color: #'.$sekcja['sectioncolor'].';">';
foreach ($sekcja['input'] as $input) {
echo '<p style="margin-left: auto; margin-right: auto; width: '.$input['inputwidth'].'px;">'.$input['inputtext'].'</p>';
}
echo '</div>';
}
echo '<div style="width: 100%; text-align: center; background-color: #eee; font-weight: 200; line-height: 0.92em; padding: 23px 0; font-size: 30px; clear: both;">Galeria zdjęć</div>';
$gallery = array_combine($_POST['srcphoto'], $_POST['descphoto']);
foreach ($gallery as $source => $alt) {
echo '<img style="width: 100%; margin-bottom: 25px;" src="'.$source.'" alt="'.$alt.'" /></img>';
}
echo '</div>';
}

在预览生成的内容时,是否可以仅保存PHP脚本生成的HTML代码?

更新#2:

<?php
error_reporting(E_ALL);
ini_set('display_errors',0); 
$tytul = $_POST['k-title'];
$opis = $_POST['k-desc'];
ob_start(); ?>
<div style="max-width: 1050px; margin-left: auto; margin-right: auto;">
<div style="padding-top: 50px; padding-bottom: 50px;">
<p style="font-size: 30px; text-align: center;"><?php echo $tytul; ?></p>
<p style="font-size: 15px; text-align: justify;"><?php echo $opis; ?></p>
</div>
<div style="width: 100%; text-align: center; background-color: #eee; font-weight: 200; line-height: 0.92em; padding: 23px 0; font-size: 30px;">Specyfikacja techniczna</div>
<table style="font-size: 14px; border: none; border-top: 1px solid #dbdbdb; border-bottom: 1px solid #dbdbdb; width: 100%;" border="0" cellspacing="0" cellpadding="5px"><tbody>
<?php 
$cechy = array_combine($_POST['cecha'], $_POST['cecha-opis']);
foreach($cechy as $klucz => $wartosc) {
?>
<tr><td style="padding: 3px 10px; text-align: right; border-right: 1px solid #dbdbdb;" width="50%">
<?php echo $klucz ?>
</td>
<td style="padding: 3px 10px; text-align: left;" width="50%">
<?php echo $wartosc; ?>
</td>
</tr>
<?php
}
?>
</tbody></table>
<?php
$prezentacje = array();
for($i = 0; $i < count($_POST['pwidth']); $i++) {
$prezentacja = array();
$prezentacja['pwidth'] = $_POST['pwidth'][$i];
$prezentacja['pheight'] = $_POST['pheight'][$i];
$prezentacja['psource'] = $_POST['psource'][$i];
array_push($prezentacje, $prezentacja);
}
foreach($prezentacje as $p) {
?>
<img style="float: left; max-width: 100%; width:<?php echo $p['pwidth']; ?>px; height: <?php echo $p['pheight']; ?>px;" src="<?php echo $p['psource']; ?>" />
<?php
}
?>
<div style="clear: both;"></div>
<?php 
$section = array();
for($a = 0; $a < count($_POST['sectionwidth']); $a++) {
$sesfull = array();
$sesfull['sectionwidth'] = $_POST['sectionwidth'][$a];
$sesfull['sectionheight'] = $_POST['sectionheight'][$a];
$sesfull['bg'] = $_POST['bg'][$a];
$sesfull['sectioncolor'] = $_POST['sectioncolor'][$a];
$sesfull['inputwidth'] = $_POST['inputwidth'][$a];
array_push($section, $sesfull);
}
foreach ($_POST['section'] as $sekcja) {
?>
<div style="margin: 0; float: left; width: <?php echo $sekcja['sectionwidth']; ?>px; height: <?php echo $sekcja['sectionheight']; ?>px; background: #<?php echo $sekcja['bg']; ?>; color: #<?php echo $sekcja['sectioncolor']; ?>;">
<?php 
foreach ($sekcja['input'] as $input) {
?>
<p style="margin-left: auto; margin-right: auto; width: <?php echo $input['inputwidth']; ?>px"> <?php echo $input['inputtext']; ?> </p>
<?php } ?>
</div>
<?php 
} 
?>
<div style="width: 100%; text-align: center; background-color: #eee; font-weight: 200; line-height: 0.92em; padding: 23px 0; font-size: 30px; clear: both;">Galeria zdjęć</div>
<?php 
$gallery = array_combine($_POST['srcphoto'], $_POST['descphoto']);
foreach ($gallery as $source => $alt) {
?>
<img style="width: 100%; margin-bottom: 25px;" src="<?php echo $source; ?>" alt="<?php echo $alt; ?>" /></img>
<?php } ?>
</div>
<?php
$szablon = ob_end_flush();
echo $szablon;
$fp = fopen('plik.html', 'w');
fwrite($fp, $szablon);
fclose($fp);
file_put_contents('plik.html', $szablon);
?>

您可以在脚本中使用输出缓冲 (http://php.net/manual/en/book.outcontrol.php(

像...

ob_start();
echo 'Some html';
$text = ob_get_flush();
echo "Content is:".$text;

将输出

Content is:Some html

更新: 如果您想捕获您生成的所有输出,ob_start();会开始缓冲(您在示例中使用它很好(,然后当您生成之后的内容时(例如(...

</tbody></table>
<?php
$content = ob_get_flush();
fwrite($fp, $content);

您必须通过Web服务器访问生成的.php脚本来获取生成的HTML(例如 http://localhost/generate.php,具体取决于代码所在的位置(。

file_get_contents('http://localhost/generate.php');

相关内容

最新更新