Is there any way to verify this file?

Asked

Viewed 50 times

0

<?php 

$ponteiro = fopen("nome.txt","r");

//LÊ O ARQUIVO ATÉ 
while (!feof ($ponteiro)) {
  //LÊ UMA
  $linha = fgets($ponteiro, 4096);
  echo "<b>".$linha."<br>";
}//FECHA WHILE

//FECHA
fclose ($ponteiro);
?>

wanted to know if there is any way to check the txt file if there is line "1" or name "dast" and play in the database

  • Put examples of the various lines you want to find in your archive

1 answer

1

See if that’s it:

<?php
$termo = "dast";
$ponteiro = fopen("nome.txt", "r");
$linha = fgets($ponteiro, 4096);
if(strpos($linha,$termo) !== false){
    echo "Econtrado";
}else{
    echo "Não existe";
}
fclose ($ponteiro);
?>

But if you want to do a DB with files I advise you to learn JSON, the search would be more accurate.

  • What if the file is a few GB in size? Can you see what would happen in your code?

  • GB of TXT, imagine initially to generate the file? The correct thing is to use DB (Mysql) so that you can manage safely, but it is probably for some test or subsystem that he is planning or creating.

  • 1

    I commented this because I think it is valid to make explicit that your solution is not feasible when the file is too large, because it stores the entire content in memory. If the goal is to parse a log file, for example, it easily passes the MB and this could make the program extremely slow. The ideal would be, as in the question code, to read the file by parts.

  • You’re absolutely right, I edited the code.

Browser other questions tagged

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