I wrote a small code in PHP in order to parse variables. I would like each time this code is called, a new line is added in the TXT file.
Each line will contain a time stamp , ie : 09:30 and a temperature value Once the TXT file is filled-in i would like to generate a Google Chart.
Time Stamps are going to be Abcisses (X) and corresponding temp value will be Y
So far, a new line is NOT created in the TXT file. Would you please help me to find out why ?
<? $File = 'live_datas.txt'; # GRAB THE VARIABLES FROM THE URL $HeuresTronconMesure = $_GET['hour']; $MinutesTronconMesure = $_GET['min']; $DonneeCapteur = $_GET['data']; # -------------------------------- $FICHIER = fopen($File, "w"); # Generate Timestamp : HH:MM fputs($FICHIER, $HeuresTronconMesure); fputs($FICHIER , ":"); fputs($FICHIER, $MinutesTronconMesure); # Add 1 space fputs($FICHIER , " "); # Add Temperature Value fputs($FICHIER, $DonneeCapteur); # Add a new line fputs ($FICHIER , "\\r\\n"); # Close the file fclose($FICHIER); ?>
At the end, i expect to get "live_datas.txt" with something like :
04:50 25.29 05:00 24.30 07:30 25.20 08:45 26.00 10:15 27.50
You have to open your file in append mode:
$FICHIER = fopen($File, "a");
What manual says about both modes:
"w" (Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist)
"a" (Write only. Opens and writes to the end of the file or creates a new file if it doesn't exist)
//call function
writeData($_GET['hour'], $_GET['min'], $_GET['data']);
//function definition
function writeData($hour, $min, $temperature) {
$path = 'live_datas.txt';
$line = "{$hour}:{$min} {$temperature}\r\n";
if (file_exists($path)) {
file_put_contents($path, $line, FILE_APPEND);
}
else {
file_put_contents($path, $line);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.