-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexport.php
More file actions
75 lines (60 loc) · 2.16 KB
/
export.php
File metadata and controls
75 lines (60 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
require_once __DIR__ . '/includes/db_mysqli.php';
// PhpSpreadsheet Library
require 'vendor/autoload.php';
// get id from url
$dictID = $_GET['dict'];
$dict_name = $_GET['name'];
$format = "xslx";
if (isset($_POST['export-format'])) {
$format = $_POST['export-format'];
}
// get each entry in the given dictionary
$sql = "SELECT lang_1, lang_2, lang_3 FROM dictionary_entries WHERE dict_id = '$dictID'";
$result = $conn->query($sql);
// create spreadsheet
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
if ($format == "xslx") {
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
} elseif ($format == "csv") {
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet);
} elseif ($format == "html") {
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Html($spreadsheet);
}
// if dictionary has any entries
if ($result->num_rows > 0) {
$i = 1;
while ($row = $result->fetch_assoc()) {
//echo "<p>".$row['lang_1']."\t".$row['lang_2']."\t".$row['lang_3']."</p>";
// write each language to the row
$spreadsheet->getActiveSheet()->setCellValue([1, $i], $row['lang_1']);
$spreadsheet->getActiveSheet()->setCellValue([2, $i], $row['lang_2']);
$spreadsheet->getActiveSheet()->setCellValue([3, $i], $row['lang_3']);
$i++;
}
}
// create exports folder if it doesn't exist
if (is_dir("exports/")) {
//echo "exports directory already exists.";
} else {
mkdir("exports/");
}
// create xlsx file and save to directory
$filePath = "exports/".$dict_name;//.".".$format;
$writer->save("$filePath");
// DOWNLOAD FILE TO CLIENT
// Check if the file exists on the server.
if (file_exists($filePath)) {
// Get the file's basename (e.g., 'myfile.txt')
$fileName = basename($filePath);
header("Content-type: " . mime_content_type($filePath));
header("Content-disposition: attachment; filename=" . $fileName); // Force download with specified filename
readfile($filePath);
exit; // Stops further execution after downloading file.
} else {
// Handle the case where the file does not exist.
echo "Error: File not found.";
}
// redirect to home page
header("Location: index.php");
?>