php - Easier method for writing to files -
is there better way of doing this?:
$datastring2 = " $leder klarte orgkrim å fikk: ".$showcah." den ".$timed." "; $datastring3 = " $hacker klarte orgkrim å fikk: ".$showcah." den ".$timed." "; $datastring4 = " $driver klarte orgkrim å fikk: ".$showcah." den ".$timed." "; $datastring5 = " $weaponexpert klarte orgkrim å fikk: ".$showcah." den ".$timed." "; $datastringinfo = "$leder, $hacker, $driver, $weaponexpert klarte å oc med å få ".$showcah." tid: ".$timed.""; $datastringinfo .= "\n"; $datastring2 .= "\n"; $datastring3 .= "\n"; $datastring4 .= "\n"; $datastring5 .= "\n"; $fwrite0 = fopen("/home/nordic/www/logger/orgkrim/completed/oversikt.txt","a"); $fwrite1 = fopen("/home/nordic/www/logger/orgkrim/oversikt.txt","a"); $fwrite2 = fopen("/home/nordic/www/logger/orgkrim/completed/$leder.txt","a"); $fwrite3 = fopen("/home/nordic/www/logger/orgkrim/completed/$hacker.txt","a"); $fwrite4 = fopen("/home/nordic/www/logger/orgkrim/completed/$driver.txt","a"); $fwrite5 = fopen("/home/nordic/www/logger/orgkrim/completed/$weaponexpert.txt","a"); $wrote2 = fwrite($fwrite2, $datastring2); $wrote2 = fwrite($fwrite3, $datastring3); $wrote3 = fwrite($fwrite4, $datastring4); $wrote4 = fwrite($fwrite5, $datastring5); $wrote5 = fwrite($fwrite1, $datastringinfo); $wrote0 = fwrite($fwrite0, $datastringinfo); fclose($fwrite0); fclose($fwrite1); fclose($fwrite2); fclose($fwrite3); fclose($fwrite4); fclose($fwrite5);
in short, sends various "logs" different places. wonder if there easier way here, save space.
comments above indicated using logger class, think isolated example overkill.
note of code doing same thing each different file: fopen
, fwrite
, fclose
. sequence of function calls ideal candidate turning function. luckily you, function exists: file_put_contents
.
$common = " klarte orgkrim å fikk: ".$showcah." den ".$timed." \n"; $datastring2 = " $leder" . $common; $datastring3 = " $hacker" . $common; $datastring4 = " $driver" . $common; $datastring5 = " $weaponexpert" . $common; $datastringinfo = "$leder, $hacker, $driver, $weaponexpert klarte å oc med å få ".$showcah." tid: ".$timed."\n"; $logdir = "/home/nordic/www/logger/orgkrim/"; file_put_contents($logdir."/completed/oversikt.txt", $datastringinfo, file_append); file_put_contents($logdir."/oversikt.txt", $datastringinfo, file_append); file_put_contents($logdir."/completed/$leder.txt", $datastring2, file_append); file_put_contents($logdir."/completed/$hacker.txt", $datastring3, file_append); file_put_contents($logdir."/completed/$driver.txt", $datastring4, file_append); file_put_contents($logdir."/completed/$weaponexpert.txt", $datastring5, file_append);
note removed many lines changing:
$foo = "bla".$bar." "; $foo .= "\n";
to
$foo = "bla".$bar." \n";
Comments
Post a Comment