I have program in linux which creates/modify a file when it detects a new connected socket. It logs the ip in that file and deletes it when the client is disconnected or disconnects..
In php i know inotify but it’s blocking and refreshes unlike java it doesn’t.. How can i do this with java and php so i can monitor a files in linux and update the website that a file in linux has been modified?
Thank you..
e.g in php..
<?php
$fd = inotify_init();
$watch_descriptor = inotify_add_watch($fd, '/tmp/devfile.txt', IN_MODIFY);
touch('/tmp/devfile.txt');
while(true){
$events = inotify_read($fd);
$contents =file_get_contents('/tmp/devfile.txt');
echo $contents;
}
$read = array($fd);
$write = null;
$except = null;
stream_select($read,$write,$except,0);
stream_set_blocking($fd, 0);
inotify_read($fd);
$queue_len = inotify_queue_len($fd);
inotify_rm_watch($fd, $watch_descriptor);
fclose($fd);
?>
I’m not sure what you mean by “java and php”, but if what you are looking for is a pure-java way to be notified of changes to the file, then you can use java.nio.file.Path and the WatchService api.
Here is an explanation from the Oracle documentation of how it works.