forked from benkeen/generatedata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.php
47 lines (38 loc) · 1.47 KB
/
generate.php
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
<?php
require_once(__DIR__ . "/library.php");
Core::init("generation");
$gen = new DataGenerator(Constants::GEN_ENVIRONMENT_POST, $_POST);
$response = $gen->generate();
if ($gen->getExportTarget() == "promptDownload") {
header("Cache-Control: private, no-cache, must-revalidate");
// check if user opted to zip the generated data
if ($gen->isPromptDownloadZipped()) {
$randNum = mt_rand(0, 100000000);
$fileName = $randNum . "_" . $response["promptDownloadFilename"];
$zipPath = $filePath . ".zip";
if (file_put_contents($fileName, $response["content"])) {
// now that we've written the file, zip it up
$zip = new ZipArchive();
$zipFile = $zip->open($zipPath, ZipArchive::CREATE);
if ($zipFile && $zip->addFile($fileName, $response["promptDownloadFilename"])) {
// we've got our zip file now we may set the response header
$zip->close();
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=" . $response["promptDownloadFilename"] . ".zip");
readfile($zipPath);
unlink($zipPath);
unlink($fileName);
exit;
}
}
// no compression, send the original data back
} else {
header("Content-Type: {$response["contentTypeHeader"]}");
if (isset($response["promptDownloadFilename"]) && !empty($response["promptDownloadFilename"])) {
header("Content-Disposition: attachment; filename={$response["promptDownloadFilename"]}");
}
echo $response["content"];
}
} else {
echo $response["content"];
}