Problems opening and closing file with fwrite

Asked

Viewed 691 times

1

Good morning, you guys! I have a problem in the part of reopening a file in Txt, the problem that in the first statement it opens normally, writes and closes. But further down in the script I try to reopen the file again and even doesn’t open, so little writes on it and returns the following error.

Warning: fwrite() expects Parameter 1 to be Resource, string Given in F: wamp64 www nfe functions.php on line 70 Warning: fclose() expects Parameter 1 to be Resource, string Given in F: wamp64 www nfe functions.php on line 71

Follow the Code:

    $pasta = $data[0].$data[1].$data[2]."_".$data_end[0].$data_end[1].$data_end[2]; 
    foreach($config->empresas as $cfg){
            $txt = fopen("sintegra/SINTEGRA_".$pasta.".txt", "w");
            fwrite($txt,
                str_pad($cfg -> reg1, 2," ", STR_PAD_RIGHT).
                str_pad($cfg -> cnpj, 14," ", STR_PAD_RIGHT).
                str_pad($cfg -> ie, 14," ", STR_PAD_RIGHT).
                str_pad($cfg -> rsocial, 35," ", STR_PAD_RIGHT).
                str_pad($cfg -> municipio, 30," ", STR_PAD_RIGHT).
                str_pad($cfg -> uf, 2," ", STR_PAD_RIGHT).
                str_pad($cfg -> fone, 10," ", STR_PAD_RIGHT).
                str_pad($data[2].$data[1].$data[0], 8, " ", STR_PAD_RIGHT).
                str_pad($data_end[2].$data_end[1].$data_end[0], 8, " ", STR_PAD_RIGHT).
                str_pad($cfg -> lreg, 3," ", STR_PAD_RIGHT).
                "\r\n".
                str_pad($cfg -> reg2, 2," ", STR_PAD_RIGHT).
                str_pad($cfg -> endereco, 34," ", STR_PAD_RIGHT).
                str_pad($cfg -> numero, 5,"0", STR_PAD_LEFT).
                str_pad($cfg -> comp, 22," ", STR_PAD_RIGHT).
                str_pad($cfg -> bairro, 15," ", STR_PAD_RIGHT).
                str_pad($cfg -> cep, 8," ", STR_PAD_RIGHT).
                str_pad($cfg -> responsavel, 28," ", STR_PAD_RIGHT).
                str_pad($cfg -> fone, 12,"0", STR_PAD_LEFT)
                );
            fclose($txt);
        }

//APENAS TESTE//
    $g_txt = "sintegra/SINTEGRA_".$pasta.".txt";
    fopen($g_txt, "a+");
    fwrite($g_txt, "aqui ficara o conteudo!");
    fclose($g_txt);

2 answers

4

This happens because the parameter that functions fwrite() and fclose() accept is the open file reference fopen() and not the string to be inserted in the file $g_txt :

 $g_txt = "sintegra/SINTEGRA_".$pasta.".txt";
 $fp = fopen($g_txt, "a+");
 fwrite($fp, "aqui ficara o conteudo!");
 fclose($fp);

2


The first time you opened the file, see that you assigned the function a name:

$txt = fopen(...

Then wrote and closed using that name:

fwrite($txt...
fclose($txt);

Hence, the error occurs because you are using the string $g_txt as function parameter. You must do as you did the first time, assign a name to the function and use this name to write/close the file:

$g_txt = "sintegra/SINTEGRA_".$pasta.".txt";
$txt = fopen($g_txt, "a+");
fwrite($txt, "aqui ficara o conteudo!");
fclose($txt);
  • Thank you my friend... had not attended me to this detail... but it was of great help.

  • 1

    Obg friend! I wish you! D

Browser other questions tagged

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