moodle/lib/filelib.php

43 lines
909 B
PHP
Raw Normal View History

2005-02-24 02:36:45 +00:00
<?php //$Id$
function get_records_csv($file, $tablename) {
global $CFG, $db;
if (!$metacolumns = $db->MetaColumns($CFG->prefix . $table)) {
return false;
}
if(!($handle = fopen($file, 'r'))) {
error('get_records_csv failed to open '.$file);
}
$fieldnames = fgetcsv($handle, 4096);
if(empty($fieldnames)) {
fclose($handle);
return false;
}
$columns = array();
foreach($metacolumns as $metacolumn) {
$ord = array_search($metacolumn->name, $fieldnames);
if(is_int($ord)) {
$columns[$metacolumn->name] = $ord;
}
}
$rows = array();
while (($data = fgetcsv($handle, 4096)) !== false) {
$item = new stdClass;
foreach($columns as $name => $ord) {
$item->$name = $data[$ord];
}
$rows[] = $item;
}
fclose($handle);
return $rows;
}
?>