What are the ways to create html file using php?

Asked

Viewed 2,032 times

4

I wanted to know the possibilities I have of creating a arquivo.html using the php.

In case I wanted to make a button when clicked generate it for me.

For example, I have a page on php and on it there is a button. By clicking this button, the code that would create this arquivo.html for me in a given directory. Then, it would be possible to access this arquivo.html via url.

What are the functions in the php that I need to use to create such a file? And if later I want to change it, have as?

  • Actually when the php code is executed an html is generated.For you to use pure html with php you have to create a file. php and ai you can use php and html code.

  • Why? I want to know the possibilities. It’s not a question based on opinions, because I don’t think there are many ways to do this. But you can always have more than one, even if there are few

  • Yes it is possible! can use file_puts_contents() or fwrite()(and the others) can do this from a template or even a textarea.

  • comrade, before I had seen this file_puts_contents() and I got interested in it. Only it resembles fwrite (the only one I knew)?

  • 1

    Good suggestion, I will specify better

  • It was good! Thank you very much :)

Show 1 more comment

1 answer

9


One of the quick ways to take advantage of an existing PHP is this:

<?php
   ob_start();                  // Isto bloqueia a saida do PHP para a "tela"

   ... tudo que você faria normalmente no PHP


   $gerado = ob_get_contents(); // Aqui capturamos o que seria enviado
   ob_end_clean();              // E limpamos, pois já está na string

   // neste momento, tudo que seria enviado para o cliente está em $gerado
   // e pode ser salvo em disco

   file_put_contents('arquivo.html', $gerado);


If you’re going to build a little more fancy application, you can avoid the output buffer and generate HTML directly in string. Instead of using echo, for example, you can concatenate in this way:

<?php
     $titulo   = 'Meu HTML gerado'; // normalmente vai pegar de DB ou formulario
     $conteudo = 'Lorem Ipsum Batatas Doces';

     // Montamos nosso HTML no PHP, da forma que quisermos
     // \t é o tab, \n a quebra de linha
     $html  = "<html>\n";
     $html .= "\t<head>\n";
     $html .= "\t\t<title>".htmlentities( $titulo )."</title>\n";
     $html .= "\t</head>\n";
     $html .= "\t<body>\n";
     $html .= "\t\t<div>".htmlentities( $conteudo )."</div>\n";
     $html .= "\t</body>\n";
     $html .= "</html>\n";

     //... e vai montando o arquivo com variáveis etc
     // e depois salva

     file_put_contents('arquivo.html', $html);         

Browser other questions tagged

You are not signed in. Login or sign up in order to post.