Re: scheduling a script to check a directory for files

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

 



On Wed, May 30, 2007 7:14 am, Bosky, Dave wrote:
> I need to write a script to import '.csv' data files into MySQL.
>
> My question is how can I have a script execute and check a directory
> every 4 hours for any '.csv' files and if it finds any calls a
> function
> to import them?

Linux: man 5 crontab
* */4 * * * /usr/local/bin/php -q import.php

Windows: Scheduled Tasks
[interminible dialogs here]

-------- import.php --------
<?php
  //Untested code
  //Needs better error handling
  //Will not scale well to *large* .csv files...
  //Use mysql LOAD DATA IN FILE for large csv files
  $path = '/path/to/directory';
  $dir = opendir($path) or die("Failed to open '$path'");
  while (($file = readdir($dir)) !== false){
    if ($file == '.' || $file == '..') continue;
    if (!preg_match('|.csv$|i', $file)){
      echo "What's '$file' doing in the .csv import directory?\n";
      continue;
    }
    $f = fopen("$path/$file", 'r');
    if (!$f){
      echo "Failed to open '$path/$file'\n";
      continue;
    }
    while (!feof($f)){
      $row = fgetcsv($f, 1000000);
      array_map('mysql_real_escape_string', $row);
      $row_sql = implode("', '", $row);
      $query = "insert into whatever values ('$row_sql')";
      mysql_query($query) or die(mysql_error());
    }
  }
?>

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
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