Guest User

Untitled

a guest
Dec 8th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. <?php
  2.  
  3. //Super Q&D. Monitors a single FTP directory for new files.
  4. //Modify the vars below and run as 'php ftpmon.php'.
  5. //Notification is via growlnotify, which requires purchase of growl.
  6.  
  7.  
  8. $host = "ftp.host.com";
  9. $user = "ftpuser";
  10. $pass = "secret";
  11. $dir = "/somedir/";
  12. $sleeptime = 60;
  13. date_default_timezone_set('America/Chicago');
  14.  
  15. function notify($title, $message) {
  16. system("growlnotify -t '$title' -m '$message' -s");
  17. }
  18.  
  19. //---------------------------------------
  20.  
  21. set_time_limit(0); //infinite
  22.  
  23. $mtime = '';
  24. $mdate = '';
  25. $mmonth = '';
  26. $mname = '';
  27. $found = false;
  28. while (!$found) {
  29. $conn = ftp_connect($host) or die("Couldn't connect\n");
  30. ftp_login($conn, $user, $pass) or die("Couldn't log in\n");
  31. $contents = ftp_rawlist($conn, "-lt $dir");
  32. sizeof($contents) or die("Couldn't get file listing\n");
  33. ftp_close($conn);
  34.  
  35. $file = explode(' ', $contents[0]); //most-recent file. Looks like this:
  36. /*
  37. array(15) {
  38. [0]=>
  39. string(10) "-rwxr--r--"
  40. [1]=>
  41. string(0) ""
  42. [2]=>
  43. string(0) ""
  44. [3]=>
  45. string(1) "1"
  46. [4]=>
  47. string(7) "user"
  48. [5]=>
  49. string(0) ""
  50. [6]=>
  51. string(7) "group"
  52. [7]=>
  53. string(0) ""
  54. [8]=>
  55. string(10) "2785658880"
  56. [9]=>
  57. string(3) "Aug"
  58. [10]=>
  59. string(2) "17"
  60. [11]=>
  61. string(5) "09:36"
  62. //12 is the filename. Anything above 12 is because the filename has spaces in it.
  63. [12]=>
  64. string(6) "pics-2"
  65. [13]=>
  66. string(1) "-"
  67. [14]=>
  68. string(13) "condo-old.tar"
  69. }
  70. */
  71. //var_dump($file);
  72.  
  73. if (strlen($mtime) == 0) {
  74. //init
  75. $mtime = $file[11];
  76. $mdate = $file[10];
  77. $mmonth = $file[9];
  78. $mname = $file[12]; //XXX Should grab everything 12+ in case of spaced filenames but this is just for UI
  79.  
  80. echo "First run. Most recent file is '$mname' at $mmonth $mdate $mtime\n";
  81. sleep($sleeptime);
  82. continue;
  83. }
  84.  
  85. if ($file[11] != $mtime) {
  86. $found = true;
  87. echo date(DATE_ATOM) . " - notifying & exiting.\n";
  88. notify('FTP site updated', $contents[0]);
  89. } else {
  90. echo date(DATE_ATOM) . " - Not found, sleeping $sleeptime.\n";
  91. sleep($sleeptime);
  92. }
  93. }
Add Comment
Please, Sign In to add comment