1

我一直在寻找一种实用的方法来检测文件系统更改。我从“Jonathan Franzone”中找到了这个非常简单的脚本。但我的问题是,它不扫描子文件夹。由于我只是 PHP 的新手,我想在这里请求提供强大的解决方案。

注意:我在写之前在网站上进行了扩展搜索。许多问题都与这种保护网站的方法有关。但根本没有完整的答复。

<?php
      /**
       * File : ftpMonitor.php
       * Monitors a remote directory via FTP and emails a list of changes if any are
       * found.
       *
       * @version June 4, 2008
       * @author <a href="http://www.franzone.com">Jonathan Franzone</a>
       */

      // Configuration ///////////////////////////////////////////////////////////////
      $host = 'ftp.domain.com';
      $port = 21;
      $user = 'username';
      $pass = 'password';
      $remote_dir = '/public_html';
      $cache_file = 'ftp_cache';
      $email_notify = 'your.email@gmail.com';
      $email_from = 'email.from@gmail.com';

      // Main Run Program ////////////////////////////////////////////////////////////

      // Connect to FTP Host
      $conn = ftp_connect($host, $port) or die("Could not connect to {$host}\n");

      // Login
      if(ftp_login($conn, $user, $pass)) {

        // Retrieve File List
        $files = ftp_nlist($conn, $remote_dir);

        // Filter out . and .. listings
        $ftpFiles = array();
        foreach($files as $file)
        {
          $thisFile = basename($file);
          if($thisFile != '.' && $thisFile != '..') {
            $ftpFiles[] = $thisFile;
          }
        }

        // Retrieve the current listing from the cache file
        $currentFiles = array();
        if(file_exists($cache_file))
        {
          // Read contents of file
          $handle = fopen($cache_file, "r");
          if($handle)
          {
            $contents = fread($handle, filesize($cache_file));
            fclose($handle);

            // Unserialize the contents
            $currentFiles = unserialize($contents);
          }
        }

        // Sort arrays before comparison
        sort($currentFiles, SORT_STRING);
        sort($ftpFiles, SORT_STRING);

        // Perform an array diff to see if there are changes
        $diff = array_diff($ftpFiles, $currentFiles);
        if(count($diff) > 0)
        {
          // Email the changes
          $msg = "<html><head><title>ftpMonitor Changes</title></head><body>" .
            "<h1>ftpMonitor Found Changes:</h1><ul>";
          foreach($diff as $file)
          {
            $msg .= "<li>{$file}</li>";
          }
          $msg .= "</ul>";
          $msg .= '<em>Script by <a href="http://www.franzone.com">Jonathan Franzone</a></em>';
          $msg .= "</body></html>";

          $headers = "MIME-Version: 1.0\r\n";
          $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
          $headers .= "To: {$email_notify}\r\n";
          $headers .= "From: {$email_from}\r\n";
          $headers .= "X-Mailer: PHP/" . phpversion();

          mail($email_notify, "ftpMonitor Changes Found", $msg, $headers);
        }

        // Write new file list out to cache
        $handle = fopen($cache_file, "w");
        fwrite($handle, serialize($ftpFiles));
        fflush($handle);
        fclose($handle);
      }
      else {
        echo "Could not login to {$host}\n";
      }

      // Close Connection
      ftp_close($conn);
      ?>

感谢任何人有解决方案或至少尝试提供帮助。

编辑:实际上我愿意要求自动删除,但是我不想要求太高。如果它不会很痛苦,为什么不呢。

4

0 回答 0