Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php
- $dirToWatch = '/mnt/files/films2/'; // don't forget trailing slash.
- $destinationDir = '/mnt/files/films/'; // don't forget trailing slash.
- $fd = null;
- $watch_descriptor = null;
- // gracefully exit handler (unregister inotify)
- function sig_handler($signo) {
- if ($fd !== null) {
- if ($watch_descriptor !== null) {
- inotify_rm_watch($fd, $watch_descriptor);
- }
- fclose($fd);
- }
- }
- pcntl_signal(SIGTERM, "sig_handler");
- pcntl_signal(SIGHUP, "sig_handler");
- pcntl_signal(SIGINT, "sig_handler");
- $fd = inotify_init();
- $watch_descriptor = inotify_add_watch($fd, $dirToWatch, IN_CREATE|IN_DELETE);
- while (true) {
- $events = inotify_read($fd);
- $filepath = $dirToWatch . $events['name'];
- $filename = $events['name'];
- // when a new file appears in dirToWatch
- if ($events['mask '] == IN_CREATE) {
- if (!is_link($destinationDir.$filename)) {
- symlink($filepath, $destinationDir.$filename);
- }
- }
- // when a file is removed from dirToWatch
- else if ($events['mask '] == IN_DELETE) {
- if (is_link($destinationDir.$filename)) {
- unlink($destinationDir.$filename);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement