Re: Parse info from 1,000 files to file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Sam Smith wrote:
> Can someone briefly point me to the functions I'll need to parse some
> information from thousands of files in a single directory and then
> prepare the extracted info into a single file for SQL import?
> 
> Like file() or readfile() and some regex and writefile??
> 
> Thanks
> 

since you did not say what type of data was in the files, I will assume it is
CSV data in my example.

<?php

$files = '/path/to/files/';

if ( is_dir($files) )
{
  if ( ($file_list = glob("{$files}*") ) !== false )
  {
    if ( !empty($file_list) )
    {
      foreach ( $file_list AS $file )
      {
        if ( ($fh = fopen($file, 'r') ) !== false )
        {
          while ( !feof($fh) ) {
            $line = fgetcsv($fh);
            # at this point, you should have your data in an array
            # process said data one line at a time.
          }
        }
      }
    }
  }
}

Another option that you could add to this is instead of cleaning your data and
inserting it into your DB, you could create a tmp file that all the data in
shoved into.  Then, once you are done processing all the original files, you
could then insert the data into your database with its LOAD FILE option.

-- 
Jim Lucas

A: Maybe because some people are too annoyed by top-posting.
Q: Why do I not get an answer to my question(s)?
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux