Skip to content

Instantly share code, notes, and snippets.

@AntonOrlov
Last active September 16, 2016 11:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AntonOrlov/b38115cdb929bf1c0dc543ebd40a489e to your computer and use it in GitHub Desktop.
Save AntonOrlov/b38115cdb929bf1c0dc543ebd40a489e to your computer and use it in GitHub Desktop.
Php web version of tail
<?php
if (isset($_GET['position'])) {
$handle = fopen('/myfile.log', 'r');
$content = stream_get_contents($handle, -1, intval($_GET['position']));
fseek($handle, 0, SEEK_END);
echo json_encode(array('position' => ftell($handle), 'content' => $content));
exit();
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
function tail(position = 0) {
setTimeout(function () {
$.getJSON('tail.php?position=' + position, function(data) {
$('#tail').append(data.content);
window.scrollTo(0, document.body.scrollHeight);
tail(data.position);
});
}, 1000);
}
tail();
</script>
</head>
<body>
<pre id="tail"></pre>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment