Laravel File Reading and Writing

Asked

Viewed 161 times

-1

I’m trying to read a file that comes from a form. When I send it to the Controller, I have to read the file and look for a line, if it exists, add a line right below.

Doubts:

1- I can find the phrase in the file, but I can’t add a line right below.

public function adiciona(Request $request){
       /*
        * O campo do form com o arquivo tinha o atributo name="file".
        */
        $file = $request->file('file');




$searchthis = "|C190|000|6102|12,00|36702,99|36702,99|4404,36|0,00|0,00|0,00|0,00||";
$matches = array();

$handle = fopen($file, "r");

if ($handle) {
      while (!feof($handle)) {
          $buffer = fgets($handle);
          if (strpos($buffer, $searchthis) !== FALSE) {
              //$matches[] = $buffer;

          }
      }
  fclose($handle);
}

//mostra os resultados:
print_r($matches);

        // Faça qualquer coisa com o arquivo enviado...
}
  • Already tried to skip the line with PHP_EOL?

  • I already managed to get the line I wanted, what is missing is to compare these two strings, to see if it exists in the file

1 answer

0

I already managed to get the line I wanted, what is missing is to compare these two strings, to see if it exists in the file

public function adiciona(Request $request){

$file = $request->file('file');
$busca ="*|C100|1|0|FOR000002301|55|00|001|48950|50190410761127000187550010000489501000489504|23042019|23042019|36702,99|1|0,00|0,00|36702,99|1|0,00|0,00|0,00|36702,99|4404,36|0,00|0,00|0,00|0,00|0,00|0,00|0,00|";
$arquivo = file($file);

// armazena o que contem a linha 11
$linha_trocada = $arquivo[2561];
$linha_compara = $arquivo[2559];
//dd($linha_compara);
var_dump($linha_compara);
if($linha_compara==$busca){
    dd("aaaaaaaaaaaaaaaaaaaaaaaa");
}
        //print_r($conteudoLinha);


}

Browser other questions tagged

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