Jump to content

Getting part of content of a file


penguine

Recommended Posts

I retrieve the content of a file.For this i use file_get_contents function.But the problem is when i want to retrieve the part of the file content,say after the last modified time.I need to retrieve the data written after the last modified time.Is it possible?

Link to comment
Share on other sites

I am checking the file periodically.Can u give me the code to extract the data written after the previous modification ?eg:i wrote content "aaaaaaaaaakkkk" to file file.txt at 12.30 pm.So the modification time of file is 12.30pm.Again i wrote content "ddddddddddddddddddddddedddddddddd" to file file.txt at 12.34 pm.I need to extract the content betwwen 12.30pm and 12.34 pm.How do i do this?

Link to comment
Share on other sites

You need to keep a record in the database or somewhere static of the last things that you wrote in the file, and then you can use either strpos or strrpos to find the first or last occurance of the last thing you wrote (hopefully the next thing you wrote isn't the same), you can do the same thing to find the position of whatever you wrote next, and then everything else is between the end of the first part and the beginning of the second part.http://www.php.net/manual/en/function.strrpos.php

Link to comment
Share on other sites

You could also set a marker at the end of the file every time you see that it has changed.The next time you read the file, see if there is anything past the marker and write a new marker for next time.A Unix timestamp on its own line might make a good marker.Just make sure that the other script doesn't add the same marker... :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...