tags:

views:

38

answers:

3

I have a directory that contains about 2000 text documents and I want to iterate through each one to parse the data. How can I do this?

A: 

scandir() will bring all filenames into an array.

array scandir ( string $directory [, int $sorting_order = 0 [, resource $context ]] )

<?php

  $dir    = '/tmp';
  $files1 = scandir($dir);
  $files2 = scandir($dir, 1);

  print_r($files1);
  print_r($files2);

?>
Jonathan Sampson
Thank you Jonathan!
John
This example is from the PHP site, please provide appropriate credit!
Michael
Thank you Jonathan. I believe this is what I need.
John
A: 

Why don't you check the PHP manual on the DirectoryIterator page? Nice class

http://php.net/manual/en/class.directoryiterator.php

The rest are trivial..

andreas
Thank for the link andreas
John
A: 

I assume you are interested in doing this in php. The key functions you will wind up using are the scandir function and the file_get_contents function.

So, you're source will look something like this:

<?php
  $my_dir_path = "/path/to/my/dir";
  $files = scandir($my_dir_path);
  $files_contents_to_array = new Array();  // will contain a mapping of file name => file contents
  if($files && count($files) > 0) {
    for($files as $file) {
      if($file /* some pattern check, verify it is indeed the file you need */) {
        $files_contents_to_array[$file] = file_get_contents($file);
      }
    }
  }
?>

I think this might be what you are looking for.

Michael