Problems opening and closing file with fwrite

1

Good morning everyone! I have a problem in the part of reopening a file in TxT, the problem that in the first statement it normally opens, writes and closes. But below in the script I try to reopen the file again and it does not even open, it just writes in 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);
    
asked by anonymous 25.04.2018 / 16:58

2 answers

2

The first time you opened the file, you have given the function a name:

$txt = fopen(...

Then wrote and closed using this name:

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

So the error occurs because you are using the string $g_txt as the function parameter. You should do as you did the first time, assign a name to the function and use that 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);
    
25.04.2018 / 17:33
4

This happens because the parameter that the fwrite() and fclose() functions accept is the reference of the opened file 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);
    
25.04.2018 / 17:34