I have PHP creating a CSV file for myself, however I'm looking to see if it's possible to create a row with titles (Latitude and longitude) on the files creation, but not in subsequent updates of the file.
<?php
$myFile = "locationlog";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, "
");
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 a file like 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 for it to create a file like 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 the titles in, but it seems to stop the PHP script from updating the file again.
EDIT:
I've tried using the filesize method.
<?php
$myFile = "locationlog";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, "
");
if(filesize($file) == 0) { fwrite($fh, "Latitude,Longitude
"); }
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 to have the opposite effect, showing this as the result:
Latitude,Longitude
Latitude,Longitude
Latitude,Longitude
Latitude,Longitude
I'm obviously doing something wrong.
EDIT 2:
I figured it out eventually. Thanks for the help! Here's the script, in case anyone needs it in the 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, "
");
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>"
?>