You are here

function search_files_list_directory in Search Files 5

search_files_list_directory($directory, $id) will be called recursively to traverse the directory tree and list all the files in the given directory, it will take the files it find and put them into the search_files_files table

Parameters

(string) $directory:

(int) $id:

1 call to search_files_list_directory()
search_files_update_index in ./search_files.module
Implementation of hook_update_index()

File

./search_files.module, line 776
Used to index all files in directory(s) on the server

Code

function search_files_list_directory($directory, $id) {
  if (!is_dir($directory)) {
    return;
  }
  watchdog('Search Files', 'Starting to list files in %directory', array(
    '%directory' => $directory,
  ));
  $file_count = 0;
  $dir_count = 0;
  if ($dir = opendir($directory)) {
    while ($file = readdir($dir)) {
      $type = filetype($directory . '/' . $file);
      $full_path = escapeshellcmd(search_files_convert_to_utf8($directory . '/' . $file));
      if ($type == 'dir') {

        // make sure we don't retreverse the current or parent directory
        if ($file != '.' && $file != '..') {
          $dir_count++;
          search_files_list_directory($directory . '/' . $file, $id);
        }
      }
      else {
        if ($type == 'file') {

          // Check to see if the file is already in the table,
          $sql = "\n          SELECT\n            id\n          FROM\n            {search_files_files}\n          WHERE\n            full_path='%s'\n        ";
          $result = db_result(db_query($sql, $full_path));

          // If the file is not in the table, insert it
          if (!$result) {
            $file_count++;
            $insert_sql = "\n            INSERT INTO\n              {search_files_files}\n            SET\n              full_path=\"%s\",\n              directory_id='%s'\n          ";

            //echo "filename: ". escapeshellcmd($file) ." - filetype: {$type}<br/>\n";

            // MySQL keeps throwing errors because of some file names that it doesn't like, this needs fixed
            $insert_result = db_query($insert_sql, $full_path, $id);
          }
        }
      }
    }
  }
  watchdog('Search Files', 'Finished Listing files in %directory, found %file_count new file(s)', array(
    '%directory' => $directory,
    '%file_count' => $file_count,
  ));
}