source: trunk/bin/file_importer.php @ 145

Last change on this file since 145 was 136, checked in by scdev, 18 years ago

Q - Merged branches/2.0singleton into trunk. Completed updating classes to use singleton methods. Implemented tests. Fixed some bugs. Changed some interfaces.

File size: 3.4 KB
Line 
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
8require realpath(dirname(__FILE__) . '/..') . '/config/cli_config.inc.php';
9
10$app =& App::getInstance();
11$db =& DB::getInstance();
12   
13
14// Test arguments.
15if ($_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;
27if (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}
69
70
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 */
83function 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?>
Note: See TracBrowser for help on using the repository browser.