Adding a row title to a csv created with PHP? -
i have php creating csv file myself, i'm looking see if it's possible create row titles (latitude , longitude) on files creation, not in subsequent updates of file.
<?php $myfile = "locationlog"; $fh = fopen($myfile, 'a') or die("can't open file"); fwrite($fh, "\r\n"); fwrite($fh, str_replace( array( 'lat=', '&lon=' ), array( '', ',' ), file_get_contents('php://input'))); fclose($fh); echo "<html><head /><body><iframe src=\"$myfile\" style=\"height:100%; width:100%;\"></iframe></body></html>" ?>
this code creates file this:
55.0203786,-7.1819549 55.0204016,-7.1819482 55.0204016,-7.1819482 55.0203927,-7.1819593 55.0203927,-7.1819593
but i'm looking create file this:
latitude,longitude 55.0203786,-7.1819549 55.0204016,-7.1819482 55.0204016,-7.1819482 55.0203927,-7.1819593 55.0203927,-7.1819593
i've tried manually putting titles in, seems stop php script updating file again.
edit:
i've tried using filesize method.
<?php $myfile = "locationlog"; $fh = fopen($myfile, 'a') or die("can't open file"); fwrite($fh, "\r\n"); if(filesize($file) == 0) { fwrite($fh, "latitude,longitude\r\n"); } elseif(filesize($file) > 0){ fwrite($fh, str_replace( array( 'lat=', '&lon=' ), array( '', ',' ), file_get_contents('php://input'))); } fclose($fh); echo "<html><head /><body><iframe src=\"$myfile\" style=\"height:100%; width:100%;\"></iframe></body></html>" ?>
it seems have opposite effect, showing result:
latitude,longitude latitude,longitude latitude,longitude latitude,longitude
i'm doing wrong.
edit 2:
i figured out eventually. help! here's script, in case needs in future.
<?php $myfile = "locationlog"; if(filesize(locationlog) == 0) { $fh = fopen($myfile, 'a') or die("can't open file"); fwrite($fh, "latitude,longitude"); } $fh = fopen($myfile, 'a') or die("can't open file"); fwrite($fh, "\r\n"); fwrite($fh, str_replace( array( 'lat=', '&lon=' ), array( '', ',' ), file_get_contents('php://input'))); fclose($fh); echo "<html><head /><body><iframe src=\"$myfile\" style=\"height:100%; width:100%;\"></iframe></body></html>" ?>
after
fwrite($fh, "\r\n");
add
fwrite($fh, "latitude,longitude\r\n");
Comments
Post a Comment