David S.
David S.

Reputation: 3

AJAX/PHP get modified contents of file

I am using PHP and AJAX requests to get the output of a program that is always running and print it on a webpage at 5 second intervals. Sometimes this log file can get up to 2mb in size. It doesn't seem practical to me for the AJAX request to fetch the whole contents of this file every 5 seconds if the user has already gotten the full contents at least once. The request just needs to get whatever contents the user hasn't gotten in a previous request.

Problem is, I have no clue on where to begin to find what contents the user hasn't received. Any hints, tips, or suggestions?

Edit: The output from the program starts off with a time (HH:MM:SS AM/PM), everything after has no pattern. The log file may span over days, so there might not be just one "02:00:00 PM" in the file, for example. I didn't write the program that is being logged, so there isn't a way for me to modify the format in which it prints it's output.

Upvotes: 0

Views: 599

Answers (2)

nebulae
nebulae

Reputation: 2665

I think using a head request might get you started along the right path.

check this thread: HTTP HEAD Request in Javascript/Ajax?

if you're using jquery, it's a simple type change in the ajax call:

$.ajax({url: "some url", type: "HEAD".....});

personally, I would check the file size & date modified against the previous response, and fetch the new data if it has been updated. I'm not sure if you can fetch only parts of a file via ajax, but I'm sure this can be accomplished via php pretty easily, possibly this thread may help:

How to read only 5 last line of the text file in PHP?

Upvotes: 2

mdziekon
mdziekon

Reputation: 3627

It depends how your program is made and how does it print your data, but you can use timestamps to reduce the amount of data. If you have some kind of IDs, you should probably use them insteam of timestamps.

Upvotes: 0

Related Questions