Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php
- <?
- /*
- Commands to install PHP-CLI, PEAR and iNotify plugin on Debian/Ubuntu:
- sudo apt-get install php5-cli php5-dev php-pear libnotify-bin
- sudo pecl install channel://pecl.php.net/inotify-0.1.4
- sudo echo "extension=inotify.so" > /etc/php5/cli/conf.d/inotify.ini
- */
- $dirToWatch = '/mnt/files/folderToWatch/'; // don't forget trailing slash.
- $destinationDir = '/mnt/files/destination/'; // don't forget trailing slash.
- $fd = null;
- $watch_descriptor = null;
- // gracefully exit handler (unregister inotify)
- function shutdown($signo) {
- global $fd, $watch_descriptor;
- print("About to gracefully exit.\n");
- if ($fd !== null) {
- if ($watch_descriptor !== null) {
- inotify_rm_watch($fd, $watch_descriptor);
- $watch_descriptor = null;
- }
- fclose($fd);
- $fd = null;
- }
- }
- pcntl_signal(SIGTERM, "shutdown");
- pcntl_signal(SIGHUP, "shutdown");
- pcntl_signal(SIGINT, "shutdown");
- $fd = inotify_init();
- $watch_descriptor = inotify_add_watch($fd, $dirToWatch, IN_CREATE|IN_DELETE|IN_MOVED_TO|IN_MOVED_FROM);
- $doContinue = true;
- while ($doContinue) {
- $events = @inotify_read($fd);
- if ($events === false) shutdown(null);
- while (count($events)>0) {
- $event = array_shift($events);
- $filename = $event['name'];
- $filepath = $dirToWatch . $filename;
- // when a new file appears in dirToWatch
- if ( ($event['mask'] & (IN_CREATE|IN_MOVED_TO)) > 0) {
- if (!is_link($destinationDir.$filename)) {
- symlink($filepath, $destinationDir.$filename);
- }
- }
- // when a file is removed from dirToWatch
- else if ( ($event['mask'] & (IN_DELETE|IN_MOVED_FROM)) > 0) {
- if (is_link($destinationDir.$filename)) {
- unlink($destinationDir.$filename);
- }
- }
- else {
- // do nothing.
- }
- }
- }
- ?>
Add Comment
Please, Sign In to add comment