GetSimple Support Forum
QUESTION external text file - Printable Version

+- GetSimple Support Forum (http://get-simple.info/forums)
+-- Forum: GetSimple (http://get-simple.info/forums/forumdisplay.php?fid=3)
+--- Forum: General Questions and Problems (http://get-simple.info/forums/forumdisplay.php?fid=16)
+--- Thread: QUESTION external text file (/showthread.php?tid=8398)



external text file - bmdt - 2016-05-06

Hello,
can i achieve this in GetSimpleCMS?

I have a temperature sensor wich puts it's last measure in a text file
Can i read this value from the text file and show it on the website?
Can anyone let me know if it's possible, and how to achieve this?

Thanks
Ben


RE: external text file - Bigin - 2016-05-06

You could add something like this in your template.php file:

Code:
$file = '/path/to/your/file.txt';
if(file_exists($file)) {
   $content = htmlspecialchars(file_get_contents($file), ENT_COMPAT, 'utf-8', false);
   echo $content;
} else {
   echo 'Error: '  .$file. 'does not exists'
}



RE: external text file - jwzumwalt - 2016-05-07

A complete example would be...




PHP Code:
<?php


  $filename 
"test.dat";
  
  
while (1) {
    $filecontents read_file($filename);
    $content htmlspecialchars($filecontentsENT_COMPAT'utf-8'false);
    echo $content;   
    flush
();
    ob_flush();    
    sleep
(60);  // read temperature every 60 seconds
  }

  /* +-------------------------------------------------------+
     |                  read file - function                 |
     |  file can be any qualified path including web urls.   |
     |  $filename = "http://www.example.com/some/file.txt"   |
     +-------------------------------------------------------+ */
    
  
function read_file($filename) {
    if (file_exists($filename)) {
      $filedata file_get_contents($filename);
      return $filedata;
    } else {
      echo "File not found";
    }
  }

?>



RE: external text file - Bigin - 2016-05-07

Infinite loops are not really a good practice server-side, you should try to avoid the while(true) idiom whenever it possible. PHP is not suitable for real time applications, Ajax call more suitable for this purpose.


RE: external text file - shawn_a - 2016-05-07

Yeah a meta refresh is just as easy if you want a live display, ideally use js or sockets, infinite flush will tie up a socket and everntually probably crash


RE: external text file - jwzumwalt - 2016-05-08

(2016-05-07, 03:12:01)Bigin Wrote: Infinite loops are not really a good practice server-side, you should try to avoid the while(true) idiom whenever it possible. PHP is not suitable for real time applications, Ajax call more suitable for this purpose.

OK... so how about an example for comparison?


RE: external text file - shawn_a - 2016-05-08

Well the poster didn't ask for a real time display, just to read a file