2017-08-04 70 views
-1

我創建了一個應用程序,它可以動態生成Javascript中的某些結構,並以結果形式將它們發送到PHP。生成器幾乎完成了,儘管我無法將生成的HTML代碼寫入文件。我有這個PHP代碼,我想將生成的HTML代碼保存到文件中。如果我用這個代碼:將生成的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); 
?> 
+1

您使用的是什麼'@ '操作員錯誤(除了使用通常是錯誤的事實之外)。 –

回答

1

您可以使用輸出緩衝在你的腳本(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 

更新: 如果您想要捕捉所有的輸出t內你產生的ob_start();開始緩衝(如果您有它在你的例子是罰款),那麼當你已經生成後的(所以舉例來說)...

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

好主意,但我不知道如何在foreach中添加元素到ob_start 看編輯主題 – Montana

+0

您可以將ob_start放在foreach之前?或者你可以將輸出存儲在數組中('$ text [] = ob_get_flush()') –

+0

你能告訴我,我怎麼能做到這一點,例如編輯中的代碼示例? – Montana

-1

你必須通過一個Web服務器(例如http://localhost/generate.php,這取決於您的代碼的位置)訪問您的generate.php腳本來獲取生成的HTML。

file_get_contents('http://localhost/generate.php'); 
相關問題