Add import_csv.php
This commit is contained in:
+162
@@ -0,0 +1,162 @@
|
||||
<?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>
|
||||
<link rel="stylesheet"
|
||||
href="assets/css/style.css">
|
||||
</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>
|
||||
Reference in New Issue
Block a user