How to create.php file using php?

Asked

Viewed 7,322 times

0

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

Do this content I should be able to, I just wanted to know how I generate this.php file using PHP itself.

  • 3

    Shouldn’t you use the database to store the information? if you want to create the file just use the file_put_content() or fopen()/fwrite()

  • https://forum.imasters.com.br/topic/427668-criar-arquivophp-com-php/

  • I don’t know, I’m new, the idea was to create the file and leave the file with the contents inside the bank, as if I had created manually, as if it were static

  • 3

    The best solution is to store it in a database. If you don’t know, this is the best time to learn. What you can do next is generate static HTML pages to serve as cache, but starts first by serving dynamic content from the database.

  • 2

    @Nicolasguilhermematheus, Cara there are even possibilities for you to do this, but it is not feasible no, your site will become very vulnerable, never allow the user himself to write a php file, php is a server-level language. Create a database where you store these texts and display the contents using templates.

  • I thought about creating a database and storing the texts there, but I don’t know how to continue the process, someone has some topic that I can study on the subject?

Show 1 more comment

1 answer

8

Basically:

<?php

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

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

Handbook:

http://php.net/manual/en/function.file-put-contents.php

Or so:

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

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

Handbook:

http://php.net/manual/en/function.fopen.php

http://php.net/manual/en/function.fwrite.php


Having responded, some considerations

That’s probably not what you want. First of all, to do what was proposed, you need to have write permissions in the directories where you will generate PHP. In addition, maintenance would be extremely painful when the content was changed. Probably, as already commented, you should think about a database option.

There are legitimate cases where generating a .html static can be interesting, and save many server resources, just by changing and/or removing these files when changing something in DB. I think a very good strategy for CMS systems, but leave it for when mastering the essential.

  • Yes, I’m thinking about the version using database, I’m studying to be able to do this, without using framework I can do this?

  • 1

    @Nicolasguilhermematheus I don’t use frameworks in PHP. Some people like it, it’s not my case. I think what’s native about it usually ends up being more efficient and straightforward to the point for just about anything. When you arrive at the part of routes (decide what to do according to the URL that the user typed), if you make a benchmark, will see that frameworks in general tend to be a "cart" compared to more straightforward solutions such as separate files and good organization. FW generally serves to "think less", and is good for those who make canned sites, generally. None of the 2 things interest me :)

Browser other questions tagged

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