Last active
September 16, 2016 11:00
-
-
Save antonorlov/b38115cdb929bf1c0dc543ebd40a489e to your computer and use it in GitHub Desktop.
Php web version of tail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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