csvxml/index6.php

95 lines
2.8 KiB
PHP

<?PHP
declare(strict_types = 1);
error_reporting(E_ALL);
ini_set('display_errors', "1");
require_once __DIR__ . "/functions/functions.php";
if (session_status() != PHP_SESSION_ACTIVE) {
session_start();
}
// This array contains all available languages
$allowed_langs = ['ar', 'de', 'en', 'hu', 'id', 'it', 'pl','pt'];
// Some languages are in translation. They will only be available for logged in users.
if (isset($_GET['navlang'])) {
$_SESSION['lang'] = $_GET['navlang'];
if (!in_array($_SESSION['lang'], $allowed_langs)) $_SESSION['lang'] = 'de';
}
else if (!isset($_SESSION['lang'])) {
$_SESSION['lang'] = lang_getfrombrowser($allowed_langs, 'en', "", false);
}
$lang = $_SESSION['lang'];
require __DIR__ . '/inc/zeichen.php';
if (empty($filename = trim($_GET['fnam'], " ,./"))) {
echo "Error: Invalid file name";
exit;
}
$csv_datei = __DIR__ . '/csv/' . $filename;
if (!file_exists($csv_datei)) {
echo "Error: The CSV file does not exist.";
exit;
}
if (is_dir(__DIR__ . "/xml")) rrmdir(__DIR__ . '/xml');
mkdir(__DIR__ . "/xml", 0755);
$fp = fopen ($csv_datei, 'r');
$y = 0;
while ($zeile = fgetcsv($fp, 100000, ';')) {
$zeile = str_replace("\xEF\xBB\xBF", '', $zeile);
$y++;
$zieldatei = 'xml/' . $y . '.xml';
if (!$handle = fopen($zieldatei, 'w')) {
echo "Cannot open file ($zieldatei)";exit;
}
$xmlDoc = new DOMDocument("1.0", "UTF-8");
$xmlDoc->preserveWhiteSpace = false;
$xmlDoc->formatOutput = true;
# $xmlDoc->encoding = 'utf-8';
$xmlMainElem = $xmlDoc->createElement("record");
$record_node = $xmlDoc->appendChild($xmlMainElem); //add record element to XML node
# $rss_node->setAttribute("version", "2.0"); //set RSS version
$lineCount = count($zeile);
for ($x = 0; $x < $lineCount; $x++) {
$inhalt[$y][$x] = $zeile[$x];
if ($inhalt[$y][$x] == '') $inhalt[$y][$x] = 'ERSATZ';
$record_node->appendChild(createTextDomElement($xmlDoc, trim($inhalt[1][$x]), trim($inhalt[$y][$x])));
# $somecontent = $somecontent . '<' . tagify(transform($inhalt[1][$x])) . '>' . (($inhalt[$y][$x])) . '</' . tagify(transform($inhalt[1][$x])) . '>' . "\n"; //für ungarn sonst weg //////////////////////////////////////////////////////////////
}
$somecontent = '<?xml version="1.0" encoding="UTF-8"?>' . $xmlDoc->saveXML($xmlDoc->documentElement);
# $somecontent .= '</record>';
//if ($y>1)
//{
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";exit;
}
fclose($handle);
}
fclose($fp);
echo printHTMLHead();
echo '
<div>
' . ($y - 1) . ' files created<br />
<hr/>
<a href="zipit.php" class="buttonLike">Download as ZIP</a>
</div>';