1

I have got this code to open each file in a folder, decrease a value by 5 and then overwrite the existing content.

<?php
    foreach (glob("users/*.php") as $filename) {    
        $array = file($filename);

        $wallet = $array[0];
        $time = $array[1];
        $status = $array[2];
        $steamid = $array[3];

    $process = fopen($filename, "w") or die("Unable to open file!");
       $time = $time - 5;
       $newdata = $wallet . "\n" . $time . "\n" . $status . "\n" . $steamid;
       fwrite($process, $newdata);
    fclose($process);
}
?>

Before I execute the script, the files that are opened look like this:

680
310
0
74892748232

After the script was executed, the file looks like this:

680

305
0

74892748232

If the script is executed again, it adds more lines and just breaks the files even more.

The string for the file is created here:

$newdata = $wallet . "\n" . $time . "\n" . $status . "\n" . $steamid;

Why does it add an empty line after $wallet and $status, but not after $time? How can I avoid this and instead write down:

$wallet
$time
$status
$steamid

Thanks a lot in advance.:)

Thomas Weiss
  • 375
  • 1
  • 2
  • 16

1 Answers1

0

Try with this solution

You can also use file_put_contents():

file_put_contents($filename, implode("\n", $array) . "\n", FILE_APPEND);

from this SO question https://stackoverflow.com/a/3066811/1301180

Community
  • 1
  • 1
Pietro
  • 988
  • 10
  • 19