SHOW:
|
|
- or go back to the newest paste.
| 1 | #!/usr/bin/php | |
| 2 | ||
| 3 | $dirToWatch = '/mnt/files/films2/'; // don't forget trailing slash. | |
| 4 | $destinationDir = '/mnt/files/films/'; // don't forget trailing slash. | |
| 5 | ||
| 6 | ||
| 7 | $fd = null; | |
| 8 | $watch_descriptor = null; | |
| 9 | ||
| 10 | // gracefully exit handler (unregister inotify) | |
| 11 | function sig_handler($signo) {
| |
| 12 | if ($fd !== null) {
| |
| 13 | if ($watch_descriptor !== null) {
| |
| 14 | inotify_rm_watch($fd, $watch_descriptor); | |
| 15 | } | |
| 16 | fclose($fd); | |
| 17 | } | |
| 18 | } | |
| 19 | pcntl_signal(SIGTERM, "sig_handler"); | |
| 20 | pcntl_signal(SIGHUP, "sig_handler"); | |
| 21 | pcntl_signal(SIGINT, "sig_handler"); | |
| 22 | ||
| 23 | ||
| 24 | $fd = inotify_init(); | |
| 25 | $watch_descriptor = inotify_add_watch($fd, $dirToWatch, IN_CREATE|IN_DELETE); | |
| 26 | while (true) {
| |
| 27 | $events = inotify_read($fd); | |
| 28 | - | $filepath = $dir . $events['name']; |
| 28 | + | $filepath = $dirToWatch . $events['name']; |
| 29 | - | $filename = basename($filepath); |
| 29 | + | $filename = $events['name']; |
| 30 | ||
| 31 | // when a new file appears in dirToWatch | |
| 32 | if ($events['mask '] == IN_CREATE) {
| |
| 33 | if (!is_link($destinationDir.$filename)) {
| |
| 34 | symlink($filepath, $destinationDir.$filename); | |
| 35 | } | |
| 36 | } | |
| 37 | // when a file is removed from dirToWatch | |
| 38 | else if ($events['mask '] == IN_DELETE) {
| |
| 39 | if (is_link($destinationDir.$filename)) {
| |
| 40 | unlink($destinationDir.$filename); | |
| 41 | } | |
| 42 | } | |
| 43 | } |