Formatting data and saving in txt with php

1

The purpose is to extract data from the base and save to txt file ... The script works correctly when I enter only $ log (name) on the line

if (!fwrite($savelog, $log))

When attempting to add $log2 (numero)

if (!fwrite($savelog, $log, $log2))

Accuse error

  

(Warning: fwrite () expects parameter 3 to be long, string given in)

What is the solution to storing the two data in txt with their proper formatting (str_pad) ?

<?php

include "conexao.php";

$querymail = mysql_query("select nome,numero from listagem");
fopen("listar.txt", "w+");
while($data = mysql_fetch_array($querymail)) {
$log = str_pad($data[nome], 30, " ", STR_PAD_RIGHT);
$log2 = str_pad($data[numero], 30, "0", STR_PAD_RIGHT);
if (!$savelog = fopen('listar.txt', "a")) 
{ exit; }
if (!fwrite($savelog, $log, $log2))
{ exit; fclose($savelog); }
}
?>
<a href="listar.txt">Download</a>
    
asked by anonymous 25.03.2016 / 22:30

1 answer

0

I do not understand your use of fwrite , as per the documentation this is the use:

/ p>

int fwrite ( resource $handle , string $string [, int $length ] )

The third parameter is expected to be an integer rather than a string.

If the $length argument is used, the writing of the document will stop after the bytes length has been written or the end of the string is reached, whichever comes first.

In other words, it is a writing "limiter".

If your goal is to write the contents of $log2 = str_pad($data[numero], 30, "0", STR_PAD_RIGHT); in .txt do so:

if ($savelog = fopen('listar.txt', "a")) {
    fwrite($savelog, $log);
    fwrite($savelog, $log2);
    fclose($savelog);
}

With concatenation:

if ($savelog = fopen('listar.txt', "a")) {
    fwrite($savelog, $log . $log2);
    fclose($savelog);
}

Or with line break:

if ($savelog = fopen('listar.txt', "a")) {
    fwrite($savelog, $log . PHP_EOL);
    fwrite($savelog, $log2);
    fclose($savelog);
}

With concatenation:

if ($savelog = fopen('listar.txt', "a")) {
    fwrite($savelog, $log . PHP_EOL . $log2);
    fclose($savelog);
}
  

Note that windows notepad.exe requires \ r \ n to support displaying line breaks, PHP_EOL works like \ n on most modern operating systems.

    
25.03.2016 / 22:40