Files
BS_Tools/SheetCheckingTool/upload.php
T
2020-12-02 15:01:32 +01:00

104 lines
2.3 KiB
PHP

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// check if file already exists
if (file_exists($target_file)) {
// echo "Sorry, file already exists.";
$uploadOk = 0;
}
// check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
// echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// allow certain file formats
if ($imageFileType != "pdf") {
// echo "Sorry, only PDF files are allowed.";
$uploadOk = 0;
}
// check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
// echo "Sorry, your file was not uploaded.";
// otherwise upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
// echo "The file " . htmlspecialchars(basename($_FILES["fileToUpload"]["name"])) . " has been uploaded.";
} else {
// echo "Sorry, there was an error uploading your file.";
}
}
// process file
$output = shell_exec("java -jar TaskSheetChecker.jar " . $target_dir . htmlspecialchars(basename($_FILES["fileToUpload"]["name"])));
$keySeparator = ',';
$delimiter = ';';
$key_array = array();
$value_array = array();
$currentString = "";
for ($i = 0; $i < strlen($output); $i++) {
if ($output[$i] == ',') {
array_push($key_array, $currentString);
$currentString = "";
} else if ($output[$i] == ';') {
array_push($value_array, $currentString);
$currentString = "";
} else {
$currentString = $currentString . $output[$i];
}
}
$emptyCounter = 0;
foreach ($value_array as &$value) {
if (strlen($value) == 0)
$emptyCounter++;
}
if ($emptyCounter > 0)
echo "Warning, there are " . $emptyCounter . " empty fields!<br>";
echo '<table style="width:100%">';
echo '<tr>';
echo '<th>Field</th>';
echo '<th>Answer</th>';
for ($i = 0; $i < count($key_array); $i++) {
echo '<tr>';
if (strlen($value_array[$i]) == 0)
echo '<th><p style="color:red">'.$key_array[$i].'</p></th>';
else
echo '<th>'.$key_array[$i].'</th>';
echo '<th>'.$value_array[$i].'</th>';
echo '</tr>';
}
echo '</table>'
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Upload PDF</title>
</head>
<body>
<?php
// TODO: php code goes here
?>
</body>
</html>