How to create .php file using php?

0

The idea is to set up a news system, once I fill out the news page and click publish, you should generate a .php file with the news content.

To do this content I should get, I just wanted to know how I can generate this .php file using PHP itself.

    
asked by anonymous 08.12.2017 / 14:56

1 answer

6

Basically:

<?php

     $conteudo = '<?php phpinfo(); ?>';

     file_put_contents('novophp.php', $conteudo);

Manual:

  

link

Or so:

$conteudo = '<?php phpinfo(); ?>';

$arquivo = fopen('novophp.php', 'w');
fwrite($arquivo, $conteudo);
fclose($arquivo);

Manual:

  

link

  

link


Having responded, some considerations

That's probably not what you want. First of all, in order to do what is proposed, you must have write permissions on the directories where PHP is to be generated. In addition, maintenance would suffer greatly when content was changed. Probably, as already commented, you should think of a database option.

There are legitimate cases where generating% static can be interesting, and saving a lot of server resources, just changing and / or removing those files when changing something in the DB. I think a very good strategy for CMS systems, but leave this to when mastering the essentials.

    
08.12.2017 / 17:17