File manipulation . txt

Asked

Viewed 462 times

1

I have a php page called "emblema.php", and a. txt file called "emblema.txt". In this file . txt, is where I put title, code and description of the emblem.

The code goes like this:

badge_name_CÓDIGO DO EMBLEMA=NOME DO EMBLEMA (TITULO DO EMBLEMA)
badge_desc_CÓDIGO DO EMBLEMA=DESCRIÇÃO DO EMBLEMA

And I wanted to know if I could create it without manually. Obviously, to add an emblem, I need to duplicate this content and change the code, title and description.

I wanted to get it added like this:

inserir a descrição da imagem aqui

  • Yes, it is possible. See functions fopen, file_put_contents, fread, fwrite (the same as the C language). They will help you.

1 answer

1

You can use the function file_put_contents to manipulate a file.

Creating a TXT file

The file_put_contents can be used to create a file with a certain content.

Example:

$filename = __DIR__ . '/arquivos/log.txt';

file_put_contents($filename, 'meu conteúdo');

The above example will create the file log.txt inside the briefcase arquivos the current directory of your script, defined by the magic constant __DIR__.

However, in this example, the file will be overwritten if it exists. And, with each new request, it will always change the content.

Adding data at the end of a TXT file

If you always want to add new content at the end of the file, you can use the flag FILE_APPEND as a parameter of file_put_contents.

Thus:

file_put_contents($filename, 'meu conteúdo', FILE_APPEND);

This will always create new content at the end of the file.

Why not use CSV?

As in your example it seems to me that you are working with data with specific organized structures, I would use a CSV.

PHP handles CSV very well.

Take a look at these explanations from the PHP Handbook:

fgetcsv

fputcsv

data serialization

From the office serialize and unserialize PHP’s can "save" variable values to later retrieve them. All you need is a file where to save this data.

Take an example.

Saving the data:

$serial = serialize($_POST);
file_put_contents('serial.txt', $serial);

Retrieving the data:

$dados = unserialize(file_get_contents('serial.txt'));
print_r($dados); // Dados anterioremente salvos vindos de $_POST

It seems to me that internally PHP uses these functions to save/recover session data ($_SESSION).

  • I do not use CSV, because the system will have to be changed completely. And like, how do I make the base to be made and the code, title and description to be multiplied as you create the others?

  • Do you want to create more than one file? Or always want to add data at the end of that file?

  • @Train did not understand why not use CSV, so that invent a new type of document, and there is one (CSV) that can be easily manipulated. In my view do not need to change anything in the answer, it is only a choice of format, you did not define what kind of format you want in the question, so in my view CSV is fully functional for your case :)

  • @Wallacemaxters The best situation is the data serialization, but this file_put_contents('serial.txt', $serial); would not divide by ID, best would be each TXT be an ID (badge code) or you provide an example that works with line breaking and use fgets+foef+while :) Just an idea.

  • file_get_contents is the simplification of fgests + foef + while. There is no reason to use them as an example. I will elaborate a way to do this whole operation with the file_get_contents and serialize.

Browser other questions tagged

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