You are here

function search_files_get_content in Search Files 7.2

return the file text using the appropriate helper

Parameters

- $path (string) the system path for the file to read:

Return value

  • the text in the file
1 call to search_files_get_content()
search_files_directories_update_index in ./search_files_directories.module
Implements hook_update_index().

File

./search_files.module, line 577
Organizes and provides helper functions for extracting text from files.

Code

function search_files_get_content($path) {
  $helpers = search_files_get_helpers();
  $file_name = explode('/', $path);
  $file_name = $file_name[count($file_name) - 1];
  $file_extension = explode('.', $file_name);
  $file_extension = $file_extension[count($file_extension) - 1];
  $quoted_file_path = '"' . escapeshellcmd(realpath($path)) . '"';
  $helper_command = preg_replace('/%file%/', $quoted_file_path, $helpers[$file_extension]);

  //error_log('$helper_command: ' . $helper_command);
  $descriptorspec = array(
    0 => array(
      "pipe",
      "r",
    ),
    // stdin is a pipe that the child will read from
    1 => array(
      "pipe",
      "w",
    ),
    // stdout is a pipe that the child will write to
    2 => array(
      "file",
      "/tmp/error-output.txt",
      "a",
    ),
  );
  $cwd = '/tmp';
  $env = array(
    'some_option' => 'aeiou',
  );
  $env = NULL;
  $process = proc_open($helper_command, $descriptorspec, $pipes, $cwd, $env);
  if (is_resource($process)) {

    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // Any error output will be appended to /tmp/error-output.txt
    $text = stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    // It is important that you close any pipes before calling
    // proc_close in order to avoid a deadlock
    $return_value = proc_close($process);
  }

  //return 'file name: ' . $quoted_file_path . ', text: ' . search_files_convert_to_utf8($text);
  return 'file name: ' . $quoted_file_path . ', text: ' . $text;
}