- Timestamp:
- Jun 15, 2006 1:10:10 PM (18 years ago)
- Location:
- trunk/bin
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bin/file_importer.php
r136 r171 6 6 */ 7 7 8 require realpath(dirname(__FILE__) . '/..') . '/config/cli_config.inc.php'; 9 10 $app =& App::getInstance(); 11 $db =& DB::getInstance(); 12 13 14 // Test arguments. 15 if ($_SERVER['argc'] == 2 && is_dir($_SERVER['argv'][1])) { 16 $file_dir = preg_replace('/\/$/', '', $_SERVER['argv'][1]); 17 } else { 18 die("You must specify the directory of files to import as the only argument.\n"); 19 } 20 21 // Get array of all files. 22 $files = getFilesRecursive($file_dir, 1); 23 24 // Loop through files. 25 // dump($files); 26 $file_count = 0; 27 if (is_array($files) && !empty($files)) { 28 foreach ($files as $file) { 29 if ($fp = fopen($file, 'r')) { 30 echo ++$file_count . "\n"; 31 $file_text = join('', file($file)); 32 fclose($fp); 33 34 // Do something with file contents. 35 preg_match('/BALANCE:\s*\$([\.\d]+)/', $file_text, $amt); 36 $file_date = date('Y-m-d', strtotime(preg_replace('|[^_]*_|', '', basename($file)))); 37 // $db->query(" 38 // INSERT INTO invoice_tbl ( 39 // client_id, 40 // invoice_type, 41 // invoice_date, 42 // invoice_amount, 43 // invoice_status, 44 // payment_type, 45 // invoice_text, 46 // email_sent_datetime, 47 // added_datetime 48 // ) VALUES ( 49 // '" . $db->escapeString(0) . "', 50 // '" . $db->escapeString('hosting') . "', 51 // '" . $db->escapeString($file_date) . "', 52 // '" . $db->escapeString($amt[1]) . "', 53 // '" . $db->escapeString('Paid') . "', 54 // '" . $db->escapeString('') . "', 55 // '" . $db->escapeString($file_text) . "', 56 // '" . $db->escapeString($file_date) . "', 57 // NOW() 58 // ) 59 // "); 60 61 } else { 62 $app->logMsg('Could not open file: ' . $file, LOG_INFO, __FILE__, __LINE__); 63 } 64 } 65 $app->logMsg('Proccessing complete: ' . $file_count . ' files total.', LOG_INFO, __FILE__, __LINE__); 66 } else { 67 $app->logMsg('No files available in that directory.', LOG_INFO, __FILE__, __LINE__); 68 } 8 // This file is only an example. 69 9 70 10 71 72 73 /** 74 * Find all files in directories recursivly with a specified file extension. 75 * 76 * @param string $dir the full path to the directory to scan 77 * @param array $valid_extensions valid extensions 78 * @param int $recurs_lev how many levels deep to scan. Set this 79 * to a low number to prevent infinite loops 80 * 81 * @return array multi-dimentional array of found files on success 82 */ 83 function getFilesRecursive($dir, $recurs_lev=0, $return=true) 84 { 85 static $output; 86 87 $dir_handle = opendir($dir); 88 while ($dir_handle && ($file = readdir($dir_handle)) !== false) { 89 if (!preg_match('/^\./', $file)) { 90 if (is_dir($dir . '/' . $file) && $recurs_lev > 0) { 91 getFilesRecursive($dir . '/' . $file, $valid_extensions, $recurs_lev - 1); 92 } else if (!is_dir($dir . '/' . $file)) { 93 $output[] = $dir . '/' . $file; 94 } 95 } 96 } 97 if ($return) { 98 return $output; 99 } 100 } 101 102 ?> 11 // require realpath(dirname(__FILE__) . '/..') . '/config/cli_config.inc.php'; 12 // 13 // $app =& App::getInstance(); 14 // $db =& DB::getInstance(); 15 // 16 // 17 // // Test arguments. 18 // if ($_SERVER['argc'] == 2 && is_dir($_SERVER['argv'][1])) { 19 // $file_dir = preg_replace('/\/$/', '', $_SERVER['argv'][1]); 20 // } else { 21 // die("You must specify the directory of files to import as the only argument.\n"); 22 // } 23 // 24 // // Get array of all files. 25 // $files = getFilesRecursive($file_dir, 1); 26 // 27 // // Loop through files. 28 // // dump($files); 29 // $file_count = 0; 30 // if (is_array($files) && !empty($files)) { 31 // foreach ($files as $file) { 32 // if ($fp = fopen($file, 'r')) { 33 // echo ++$file_count . "\n"; 34 // $file_text = join('', file($file)); 35 // fclose($fp); 36 // 37 // // Do something with file contents. 38 // preg_match('/BALANCE:\s*\$([\.\d]+)/', $file_text, $amt); 39 // $file_date = date('Y-m-d', strtotime(preg_replace('|[^_]*_|', '', basename($file)))); 40 // // $db->query(" 41 // // INSERT INTO invoice_tbl ( 42 // // client_id, 43 // // invoice_type, 44 // // invoice_date, 45 // // invoice_amount, 46 // // invoice_status, 47 // // payment_type, 48 // // invoice_text, 49 // // email_sent_datetime, 50 // // added_datetime 51 // // ) VALUES ( 52 // // '" . $db->escapeString(0) . "', 53 // // '" . $db->escapeString('hosting') . "', 54 // // '" . $db->escapeString($file_date) . "', 55 // // '" . $db->escapeString($amt[1]) . "', 56 // // '" . $db->escapeString('Paid') . "', 57 // // '" . $db->escapeString('') . "', 58 // // '" . $db->escapeString($file_text) . "', 59 // // '" . $db->escapeString($file_date) . "', 60 // // NOW() 61 // // ) 62 // // "); 63 // 64 // } else { 65 // $app->logMsg('Could not open file: ' . $file, LOG_INFO, __FILE__, __LINE__); 66 // } 67 // } 68 // $app->logMsg('Proccessing complete: ' . $file_count . ' files total.', LOG_INFO, __FILE__, __LINE__); 69 // } else { 70 // $app->logMsg('No files available in that directory.', LOG_INFO, __FILE__, __LINE__); 71 // } 72 // 73 // 74 // 75 // 76 // /** 77 // * Find all files in directories recursivly with a specified file extension. 78 // * 79 // * @param string $dir the full path to the directory to scan 80 // * @param array $valid_extensions valid extensions 81 // * @param int $recurs_lev how many levels deep to scan. Set this 82 // * to a low number to prevent infinite loops 83 // * 84 // * @return array multi-dimentional array of found files on success 85 // */ 86 // function getFilesRecursive($dir, $recurs_lev=0, $return=true) 87 // { 88 // static $output; 89 // 90 // $dir_handle = opendir($dir); 91 // while ($dir_handle && ($file = readdir($dir_handle)) !== false) { 92 // if (!preg_match('/^\./', $file)) { 93 // if (is_dir($dir . '/' . $file) && $recurs_lev > 0) { 94 // getFilesRecursive($dir . '/' . $file, $valid_extensions, $recurs_lev - 1); 95 // } else if (!is_dir($dir . '/' . $file)) { 96 // $output[] = $dir . '/' . $file; 97 // } 98 // } 99 // } 100 // if ($return) { 101 // return $output; 102 // } 103 // } 104 // 105 // ?>
Note: See TracChangeset
for help on using the changeset viewer.