agentaustin Posted December 17, 2010 Share Posted December 17, 2010 So here is what I have going. #1 - My weather station software processes a file and enters variables and then uploads it as a php file. #2 - Currently the station also just uploads every file to the site and enters the data in the html, but I would rather have it only upload this one file and the other pages grab data from it and print it out. I have a few php scrips in there to preform certain functions (if temp <= 32 it echos that data in blue and so on) but would rather make most of it php so I have less files being uploaded every 5 minutes. This is what the station sends to the web, but I have no idea how to get the other pages to read it and input the variables from it. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/221956-reading-variables-from-another-file/ Share on other sites More sharing options...
litebearer Posted December 17, 2010 Share Posted December 17, 2010 I am hoping someone (Pika maybe) can offer a better solution. if the file is consistently the same (ie only the values of the variables will change, but location of variables, number of variables, etc etc remain constant) this may help get you started <?PHP /* this is the name of the raw data file the station uploads */ $myFile = "cumulus_raw.php"; $lines = file($myFile); $w = count($lines) - 1; /* if the raw data file is ALWAYS consistent in structure this should be where the first variable starts */ $start = 49; /* this is used to get rid of lines containing the ============== */ $needle = "===="; while($start<$w) { $show = 1; $ww = strlen(trim($lines[$start])); if($ww<1) { /* this removes empty lines */ $show = 0; } $pos = strpos($lines[$start],$needle); if( $pos === false) { }else{ $show = 0; } if($show == 1) { /* this creates a new array containing only valid information */ $new_lines[] = $lines[$start]; } $start ++; } $data = implode("\n",$new_lines); /* this condenses the new array into 1 long string with appropriate new lines */ $data = "<?PHP" . "\n" . $data . "?>"; /* this adds the opening and closing php tags */ $ts = time(); $file = $ts . '.txt'; /* this creates the file name to be used by your other scripts */ file_put_contents($file, $data); /* this does the actual writing to the file */ include($file); /* this is simply showing how you can INCLUDE the newly created file to access the variables */ echo "Today's high temperature of " . $tempTH . " occured at " . $TtempTH; ?> Quote Link to comment https://forums.phpfreaks.com/topic/221956-reading-variables-from-another-file/#findComment-1148767 Share on other sites More sharing options...
agentaustin Posted December 18, 2010 Author Share Posted December 18, 2010 Only issue I see is that every time the page is loaded it writes a new file, so with the volume of traffic I get I'm going to have a ton of files. Quote Link to comment https://forums.phpfreaks.com/topic/221956-reading-variables-from-another-file/#findComment-1148916 Share on other sites More sharing options...
litebearer Posted December 18, 2010 Share Posted December 18, 2010 How often does the weather station upload? Is there are reason you are not adding the information to a database? Rather than creating a new file, you could store the information into a database table (one of the table fields could be the timestamp of when the data was added to the table), that would eliminate having so many files. Quote Link to comment https://forums.phpfreaks.com/topic/221956-reading-variables-from-another-file/#findComment-1148989 Share on other sites More sharing options...
anandgodar Posted December 18, 2010 Share Posted December 18, 2010 reading and writing from file is also better but why dont you use session and after printing the values unset the sesion Quote Link to comment https://forums.phpfreaks.com/topic/221956-reading-variables-from-another-file/#findComment-1149018 Share on other sites More sharing options...
agentaustin Posted December 19, 2010 Author Share Posted December 19, 2010 Station uploads that file every 48 seconds, but currently the pages only get updated every 5 minutes to reduce FTP traffic. Quote Link to comment https://forums.phpfreaks.com/topic/221956-reading-variables-from-another-file/#findComment-1149180 Share on other sites More sharing options...
Anti-Moronic Posted December 19, 2010 Share Posted December 19, 2010 I would put this down to bad design. You shouldn't have to upload a file every 48 seconds to maintain data. You then have to convert these files? I can only assume there is a very good reason you have not tried to use a database. Another idea - you could use http://www.sqlite.org/ as an alternative. Again though, the main issue here is that you are using FTP. Quote Link to comment https://forums.phpfreaks.com/topic/221956-reading-variables-from-another-file/#findComment-1149183 Share on other sites More sharing options...
agentaustin Posted December 20, 2010 Author Share Posted December 20, 2010 That is limitations of the station, it can only process files and upload via FTP. Quote Link to comment https://forums.phpfreaks.com/topic/221956-reading-variables-from-another-file/#findComment-1149416 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.