Example:
Code: Select all
1.00 2.00 3.00 4.00 5.00 6.00
Code: Select all
2.00 3.00 4.00 5.00 6.00 7.00
Thanks all!
Mike
Moderator: daj
Code: Select all
1.00 2.00 3.00 4.00 5.00 6.00
Code: Select all
2.00 3.00 4.00 5.00 6.00 7.00
Code: Select all
$DATA = preg_split('/ +/', $rawdata[0]);
$fp = fopen( $SITE['datafile'] , 'a' );
if ($fp) {
$info = "";
$first = 1;
foreach($DATA as $key) {
$info .= " ";
$info .= $key; }
fwrite($fp, $info . "\n" ) ;
fclose($fp);
}
Code: Select all
<?php
$raw = file('realtime.txt');
$rawdata = array();
$rawdata = explode ( ' ', $raw[0] );
$latest_rain = $rawdata[9];
$rainraw = file('rain.log');
$rainraw[0] = rtrim($rainraw[0]);
$raindata = array();
$raindata = explode(' ', $rainraw[0]);
$x = count($raindata);
$output = "";
if($x > 5){
$xFrom = 1;
$xTo = 6;
} else {
$xFrom = 0;
$xTo = $x;
}
for( $rebuild = $xFrom; $rebuild < $xTo ; $rebuild++) {
$output .= $raindata[$rebuild]. ' ';
}
$output .= $latest_rain. "\n";
$fh = fopen("rain.log", "w");
fwrite($fh, $output);
fclose($fh);
?>
Code: Select all
$x2 = ($x <6 ? $x : 6);
Not sure what you are doing here. I am not too good with php yet.Parse error: syntax error, unexpected T_VARIABLE
Warning: explode() expects parameter 2 to be string, array given in /home/bla/public_html/bla/test.php on line 5
Warning: explode() expects parameter 2 to be string, array given in /home/bla/public_html/bla/test.php on line 10
Code: Select all
<?php
##############################################################################################
#
//error_reporting(E_NOTICE);
//error_reporting(E_ERROR);
error_reporting(E_ALL);
//error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
//error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);
//ini_set('log_errors', 1);
//ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
#
##############################################################################################
$DoDebug = true;
$realtime = trim('realtime.txt');
$rainfile = trim('rain.log');
if (!file_exists($realtime)) { exit("ERROR: File '$realtime' was not found! Exiting!");}
if (!file_exists($rainfile)) { exit("ERROR: File '$rainfile' was not found! Exiting!");}
// getting data from 'realtime.txt' file
$realtimedata = array();
$raw = trim(file_get_contents($realtime));
// splitting string into an array
$realtimedata = explode(' ', $raw);
$rain = $realtimedata[9]; // rain today
$uomRain = $realtimedata[16]; // rain unit - not needed
if ($DoDebug) { print "<br /><br />realtime file:"; var_dump ($realtimedata); }
if ($DoDebug) { print "<br />Rain value from realtime file: $rain $uomRain<br />"; }
// getting rain data from 'rain.log' file
$raindata = array();
$raw = trim(file_get_contents($rainfile));
// splitting string into an array
$raindata = explode (' ', $raw);
if ($DoDebug) { print "<br />Old rain-log file:"; var_dump ($raindata); }
// shifting first value off - shortening array by one element
$temp = array_shift($raindata);
if ($DoDebug) { print "<br />Shortened rain-log file:"; var_dump($raindata); }
// pushing new variable onto the end of array - increasing length of array by one element
$temp = array_push($raindata, $rain);
if ($DoDebug) { print "<br />New rain-log file:"; var_dump($raindata); }
// join array elements into a string
$output = implode(' ', $raindata);
if ($DoDebug) { print "<br />New data to be written to rain-log file: $output<br />"; }
// writing string to file - simple option
if (file_put_contents($rainfile, $output) === FALSE) { exit("ERROR: Cannot write to file '$rainfile'! Exiting!"); }
if ($DoDebug) { print "<br />Success: '$output' was written to file '$rainfile'"; }
// writing string to file - alternate option
//if (is_writable($rainfile)) {
// if (!$handle = fopen($rainfile, 'w')) { exit("ERROR: Cannot open file '$rainfile'! Exiting!"); }
// if (fwrite($handle, $output) === FALSE) { exit("ERROR: Cannot write to file '$rainfile'! Exiting!"); }
// if ($DoDebug) { print "<br />Success: '$output' was written to file '$rainfile'"; }
// fclose($handle);
//}
//else { print "WARNING: The file '$rainfile' is not writable!"; }
exit;
##############################################################################################
?>
Code: Select all
$HOME = "/home/path/to/log/and/txt/";
chdir($HOME);
which didn't resolve it. but I will keep on it.$rawdata = explode ( ' ', $raw[0] );
and
$raindata = explode(' ', $rainraw[0]);
Code: Select all
$DoDebug = true;
$realtime = 'realtime.txt';
$rainfile = 'rain.log';
$webDir = 'cumulus';
// Obtain basic environment
if (!isset($_SERVER['DOCUMENT_ROOT'])) {
$path_trans = str_replace('\\\\', '/', $_SERVER['PATH_TRANSLATED']);
$ROOTDIR = substr($path_trans, 0, 0 - strlen($_SERVER['PHP_SELF']));
}
else { $ROOTDIR = $_SERVER['DOCUMENT_ROOT']; }
$fileDir = realpath($ROOTDIR.'\\'.trim($webDir)).'\\';
$realtime = $fileDir.trim($realtime);
$rainfile = $fileDir.trim($rainfile);
if (!file_exists($realtime)) { exit("ERROR: File '$realtime' was not found! Exiting!");}
if (!file_exists($rainfile)) { exit("ERROR: File '$rainfile' was not found! Exiting!");}
...
Its working fine above the root directory. I have it up there because I need to avoid bots hitting the file (robots.txt is only obeyed by legit bots!) and triggering it thus messing up the days. I have a cron job triggering it once a day. If it was to trigger twice, then the log is off a day or worse if it rains lets say .25 inches that day and it logs that .25 twice.It is odd to read about putting some code/script above the highest directory