599f38f9 |
1 | <?php //$Id$ |
2 | |
3 | function get_records_csv($file, $tablename) { |
4 | global $CFG, $db; |
5 | |
6 | if (!$metacolumns = $db->MetaColumns($CFG->prefix . $table)) { |
7 | return false; |
8 | } |
9 | |
10 | if(!($handle = fopen($file, 'r'))) { |
11 | error('get_records_csv failed to open '.$file); |
12 | } |
13 | |
14 | $fieldnames = fgetcsv($handle, 4096); |
15 | if(empty($fieldnames)) { |
16 | fclose($handle); |
17 | return false; |
18 | } |
19 | |
20 | $columns = array(); |
21 | |
22 | foreach($metacolumns as $metacolumn) { |
23 | $ord = array_search($metacolumn->name, $fieldnames); |
24 | if(is_int($ord)) { |
25 | $columns[$metacolumn->name] = $ord; |
26 | } |
27 | } |
28 | |
29 | $rows = array(); |
30 | |
31 | while (($data = fgetcsv($handle, 4096)) !== false) { |
32 | $item = new stdClass; |
33 | foreach($columns as $name => $ord) { |
34 | $item->$name = $data[$ord]; |
35 | } |
36 | $rows[] = $item; |
37 | } |
38 | |
39 | fclose($handle); |
40 | return $rows; |
41 | } |
42 | |
43 | ?> |