Пример #1
0
function import_csv($file_path)
{
    $file_name = basename($file_path);
    // Enter main loop.
    if (!($fp = fopen($file_path, 'r'))) {
        die("Error: File cannot be opened.\n");
    }
    $header = fgetcsv($fp, 4056);
    $rows = array();
    if (($pos = array_search('id', $header)) !== false) {
        $header[$pos] = 'item_id';
    }
    while ($row = fgetcsv($fp, 8112)) {
        $rows[] = array_combine($header, $row);
    }
    fclose($fp);
    $table_name = preg_replace('/[^a-zA-Z0-9_]+/', '', str_ireplace('.csv', '', $file_name));
    if (preg_match('/(_[0-9]+_[0-9]+)$/', $table_name, $matches)) {
        $table_name = str_replace($matches[1], '', $table_name);
    }
    $profile = profile_resultset($rows);
    $table_sql = make_table_sql($table_name, $profile);
    kill_table($table_name);
    $res = mysql_query($table_sql);
    if (!$res) {
        throw new Exception("DB Error: " . mysql_error());
    }
    bulk_insert($table_name, $rows);
}
Пример #2
0
function read_csv($filename)
{
    $f = fopen($filename, "r");
    $row = 0;
    while (!feof($f)) {
        $fcontent = fgetcsv($f);
        if ($row == 0) {
            $row = 1;
            continue;
        }
        if (count($fcontent) == 3) {
            $query[] = "('" . implode("','", $fcontent) . "')";
        }
        if (count($query) == 1000) {
            bulk_insert($query);
            $query = '';
        }
    }
    return $query;
}