3

I have a text file storing strings. The text file will be changing every 1 minute. I want to show whole string in my php page. My php code just fetchs the data from text file. I want my php page to refresh every minute and show the updated data.

My data.txt file is:

1~1~10,56,82,34,22,78,56,15,41,25,47,33,48~82-I am Aakash,83- I am Vijay

my php code for fetching data is:

<?php
$myFile = "data.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;
?> 
Hüseyin BABAL
  • 15,400
  • 4
  • 51
  • 73
user3226468
  • 95
  • 1
  • 2
  • 6
  • you will need a server.php file, which will monitor the modification timestamp of the .txt file, and if needed, generates the output. On your web page (index.php), you should put an ajax call to server.php. From it's response, you will update the page using jQuery. (this would be my approach) – robi Jan 24 '14 at 06:37
  • No need to use PHP; use Javascript and AJAX. – Rahul Desai Jan 24 '14 at 06:38
  • Possible duplicate: http://stackoverflow.com/questions/14446447/javascript-read-local-text-file – Rahul Desai Jan 24 '14 at 06:43

1 Answers1

4

You can use stream_get_contents. Simply, stream your text file like tailing. Your client html will make ajax call every minute to your server side script written in php. For example;

PHP: file_read.php

<?php
if (isset($_GET['tail'])) {
  session_start();
  $handle = fopen('your_txt_file.txt', 'r');// I assume, a.txt is in the same path with file_read.php
  if (isset($_SESSION['offset'])) {
    $data = stream_get_contents($handle, -1, $_SESSION['offset']);// Second parameter is the size of text you will read on each request
    echo nl2br($data);
  } else {
    fseek($handle, 0, SEEK_END);
    $_SESSION['offset'] = ftell($handle);
  } 
  exit();
} 
?>

HTML:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <script src="jquery.min.js"></script><!-- give corrected jquery path -->
  <script>
  setInterval(function(){get_contents();}, 10000*60);
  function get_contents() {
    $.get('file_read.php.php?tail', function(data) {
        $('#contents').append(data);
      });
  }
  </script>
</head>
<body>
  <div id="contents">Loading...</div>
</body>
</html>
Hüseyin BABAL
  • 15,400
  • 4
  • 51
  • 73
  • Thank You sir, but my text file is put in c drive which are change every minute and i want to fetch data from text file and show on web page,and also i want my web page refresh every minute.means show updated data infront of user – user3226468 Jan 24 '14 at 06:56
  • Ok, your file is on c drive. And your web page runs on local. Then you need to give full path in my code by replacing `your_txt_file.txt` with `c:\full_path\your.txt`. The code in my answer also makes ajax call in every minute – Hüseyin BABAL Jan 24 '14 at 07:29
  • Look at the firebug network console. Does it sends request to backend? And firebug console for any js error. Be sure that you have included jquery – Hüseyin BABAL Jan 24 '14 at 07:51
  • i will check but i will not getting anything please help me – user3226468 Jan 24 '14 at 08:29
  • please see my edited question and give me appropriate answer.thankyou – user3226468 Jan 24 '14 at 08:30
  • what offset i will give – user3226468 Jan 24 '14 at 08:36