PHPで自分用のCSVファイルを作成していますが、ファイルの作成時にタイトル(緯度と経度)を含む行を作成できるかどうかを確認していますが、その後のファイルの更新では作成できません。
<?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>"
?>
このコードは、次のようなファイルを作成します。
55.0203786,-7.1819549
55.0204016,-7.1819482
55.0204016,-7.1819482
55.0203927,-7.1819593
55.0203927,-7.1819593
しかし、私は次のようなファイルを作成するためにそれを探しています:
Latitude,Longitude
55.0203786,-7.1819549
55.0204016,-7.1819482
55.0204016,-7.1819482
55.0203927,-7.1819593
55.0203927,-7.1819593
タイトルを手動で入れようとしましたが、PHP スクリプトがファイルを再び更新するのを止めているようです。
編集:
ファイルサイズメソッドを使用してみました。
<?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>"
?>
これは逆の効果があるようで、結果として次のようになります。
Latitude,Longitude
Latitude,Longitude
Latitude,Longitude
Latitude,Longitude
私は明らかに何か間違ったことをしています。
編集2:
私は最終的にそれを理解しました。助けてくれてありがとう!将来誰かが必要になった場合に備えて、スクリプトを次に示します。
<?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>"
?>