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