This commit is contained in:
Dávid Jančár 2024-05-29 12:07:26 +02:00
parent 274c93b86f
commit c77cfeab66
4 changed files with 79 additions and 39 deletions

@ -1,37 +1,78 @@
<?php <?php
$skeleton = file_get_contents('templates/skeleton.html');
$menu_obsah = file_get_contents('templates/header.html'); if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["submit"])) {
$out = str_replace('__TEMPLATE_HEADER__', $menu_obsah, $skeleton); $target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
$footer_obsah = file_get_contents('templates/footer.html'); // Check if file already exists
$out = str_replace('__TEMPLATE_FOOTER__', $footer_obsah, $out); if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
$nazov_stranky = basename($_SERVER['QUERY_STRING']); // Check file size
if (empty($nazov_stranky)){ if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$out = "The file " . htmlspecialchars(basename($_FILES["fileToUpload"]["name"])) . " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
} else {
$skeleton = file_get_contents('templates/skeleton.html');
$menu_obsah = file_get_contents('templates/header.html');
$out = str_replace('__TEMPLATE_HEADER__', $menu_obsah, $skeleton);
$footer_obsah = file_get_contents('templates/footer.html');
$out = str_replace('__TEMPLATE_FOOTER__', $footer_obsah, $out);
$nazov_stranky = basename($_SERVER['QUERY_STRING']);
if (empty($nazov_stranky)) {
$nazov_stranky = 'index'; $nazov_stranky = 'index';
} }
if (!empty($_GET["listfiles"])) {
$files = scandir($target_dir);
$out = "<tr><th>Názov súboru</th><th>Download</th></tr>";
foreach ($files as $file) {
$filename = $file;
$filepath = "/uploads/$file";
$out .= "<tr><td>SuperSubor</td><td><a href='$filepath' download>$filename</a></td></tr>";
}
} else {
$cesta_k_stranke = "stranky/$nazov_stranky.html"; $cesta_k_stranke = "stranky/$nazov_stranky.html";
if (!file_exists($cesta_k_stranke)) { if (!file_exists($cesta_k_stranke)) {
$cesta_k_stranke = "templates/404.html"; $cesta_k_stranke = "templates/404.html";
} }
$stranka_obsah = file_get_contents($cesta_k_stranke); $stranka_obsah = file_get_contents($cesta_k_stranke);
$out = str_replace('__TEMPLATE_STRANKA__', $stranka_obsah, $out); $out = str_replace('__TEMPLATE_STRANKA__', $stranka_obsah, $out);
$cesta_k_skriptu = "scripty/$nazov_stranky.js"; $cesta_k_skriptu = "scripty/$nazov_stranky.js";
$script_obsah = "<script src=\"$cesta_k_skriptu\"></script>"; $script_obsah = "<script src=\"$cesta_k_skriptu\"></script>";
if (!file_exists($cesta_k_skriptu)){ if (!file_exists($cesta_k_skriptu)) {
$script_obsah = ''; $script_obsah = '';
} }
$out = str_replace('__TEMPLATE_SCRIPT__', $script_obsah, $out); $out = str_replace('__TEMPLATE_SCRIPT__', $script_obsah, $out);
$cesta_k_stylu = "styles/$nazov_stranky.css"; $cesta_k_stylu = "styles/$nazov_stranky.css";
$style_obsah = "<link rel=\"stylesheet\" href=\"$cesta_k_stylu\">"; $style_obsah = "<link rel=\"stylesheet\" href=\"$cesta_k_stylu\">";
if (!file_exists($cesta_k_stylu)){ if (!file_exists($cesta_k_stylu)) {
$style_obsah = ''; $style_obsah = '';
}
$out = str_replace('__TEMPLATE_STYLE__', $style_obsah, $out);
}
echo $out;
} }
$out = str_replace('__TEMPLATE_STYLE__', $style_obsah, $out);
echo $out;
?>

@ -1,5 +1,5 @@
document.onload = function () { document.onload = function () {
const response = fetch("/upload.php"); const response = fetch("/index.php?listfiles=1");
const tabulka = document.getElementById("tabulka"); const tabulka = document.getElementById("tabulka");
tabulka.innerHTML = response.text; tabulka.innerHTML = response.text;
} }

@ -1,6 +1,6 @@
<table id="tabulka"></table> <table id="tabulka"></table>
<form action="/upload.php" method="post" enctype="multipart/form-data"> <form action="/index.php" method="post" enctype="multipart/form-data">
Vyber súbor na nahratie: Vyber súbor na nahratie:
<input type="file" name="fileToUpload" id="fileToUpload"> <input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Nahrať súbor" name="submit"> <input type="submit" value="Nahrať súbor" name="submit">

@ -1,11 +1,10 @@
<?php <?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image // Check if image file is a actual image or fake image
if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["submit"])) { if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["submit"])) {
$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 // Check if file already exists
if (file_exists($target_file)) { if (file_exists($target_file)) {