Files
woordjes/import_csv.php
T
2026-06-16 11:24:18 +02:00

170 lines
2.6 KiB
PHP

<?php
require_once 'includes/config.php';
require_once 'includes/auth.php';
require_once 'includes/functions.php';
require_once 'includes/jsondb.php';
requireLogin();
$lang =
$_GET['lang'] ?? '';
$list =
$_GET['list'] ?? '';
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (
!isset($_FILES['csv'])
) {
$error = 'Geen bestand';
} else {
$tmp =
$_FILES['csv']['tmp_name'];
$handle =
fopen($tmp, 'r');
if (!$handle) {
die('CSV fout');
}
$header =
fgetcsv($handle);
$words = [];
$id = 1;
while (
($row = fgetcsv($handle))
!== false
) {
$words[] = [
'id' => $id++,
'question' =>
trim($row[0]),
'answer' =>
trim($row[1]),
'correct' => 0,
'wrong' => 0
];
}
fclose($handle);
JsonDB::write(
listPath(
$_SESSION['user'],
$lang,
$list
),
[
'metadata' => [
'title' => $list,
'language' =>
$lang,
'created' =>
date('c'),
'wordCount' =>
count($words)
],
'words' => $words
]
);
header(
'Location: language.php?lang=' .
urlencode($lang)
);
exit;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>CSV upload</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#222">
<link rel="manifest" href="/manifest.json">
<link rel="stylesheet" href="/assets/css/style.css">
<link rel="apple-touch-icon" sizes="180x180" href="/assets/img/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/assets/img/favicon.ico">
<link rel="icon" type="image/png" sizes="16x16" href="/assets/img/favicon-16x16.png">
</head>
<body>
<div class="container">
<div class="card">
<h1>CSV upload</h1>
<p>
Taal:
<?= htmlspecialchars($lang) ?>
</p>
<p>
Lijst:
<?= htmlspecialchars($list) ?>
</p>
<form
method="post"
enctype="multipart/form-data">
<input
type="file"
name="csv"
accept=".csv"
required>
<button>
Importeren
</button>
</form>
<p>
CSV formaat:
</p>
<pre>
question,answer
dog,hond
cat,kat
horse,paard
</pre>
</div>
</div>
</body>
</html>