PHP

HOW TO WRITE & APPEND TO FILES IN PHP

WILL OVERRIDE IF FILE EXISTS file_put_contents("FILE", "STRING");

FILE PUT CONTENTS

01

TO APPEND file_put_contents("FILE", "STRING", FILE_APPEND);

OPEN FILE $fh = fopen("FILE.TXT", "w");   USE "A" TO APPEND INSTEAD // $fh = fopen("FILE.TXT", "a");

FILE OPEN WRITE CLOSE

02

WRITE TO FILE fwrite($fh, "STRING");   CLOSE FILE fclose($fh);

ARRAY OF DATA $data = ["Apple", "Beet", "Cherry"];   OPEN FILE $fh = fopen("FILE", "w");

ARRAYS & LINE BREAKS

03

WRITE TO FILE \r\n FOR NEW LINE foreach ($data as $d) { fwrite($fh, "$d\r\n"); }   CLOSE FILE fclose($fh);