Perform the upload of the file

This commit is contained in:
Pierre HUBERT 2025-01-31 11:23:17 +01:00
parent 4788ac1685
commit 20a7eeb659
4 changed files with 81 additions and 31 deletions

1
unsafe_gallery/src/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
uploads

View File

@ -0,0 +1 @@
Options -Indexes

View File

@ -1,34 +1,72 @@
<?php
print_r($_FILES);
session_start();
if(isset($_FILES["file"]))
{
TODO
// Assign unique session ID to the client
if (!isset($_SESSION["id"]))
$_SESSION["id"] = uniqid();
// Specify uploads target directory
define('UPLOAD_DIR', __DIR__ . "/uploads/" . $_SESSION["id"]);
if (isset($_FILES["file"])) {
$dest_file_name = (string)time() ."-". str_replace("/", "", $_FILES["file"]["name"]);
// Create target directory
if(!is_dir(UPLOAD_DIR) && !mkdir(UPLOAD_DIR, 0770, true)) {
$error = "Failed to create storage directory!";
}
else if ($_FILES["file"]["size"] > 10000) {
$error = "File is too large!";
} else if (move_uploaded_file($_FILES["file"]["tmp_name"], UPLOAD_DIR . "/" . $dest_file_name)) {
$success = "The file was successfully uploaded!";
} else {
$error = "Error while uploading file!";
}
}
?><!doctype html>
<html lang="en" data-bs-theme="auto">
<head>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Safe gallery</title>
<link href="/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link href="/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link href="/style.css" rel="stylesheet">
</head>
<body class="d-flex align-items-center py-4 bg-body-tertiary">
<main class="form-signin w-100 m-auto">
</head>
<body class="d-flex align-items-center py-4 bg-body-tertiary">
<main class="form-signin w-100 m-auto">
<div class="alert alert-success">
<strong>Note</strong> : Une information se cache dans la variable d'environnement <i>FLAG</i>.
</div>
<h1>Gallery manager</h1>
<h2>Upload file</h2>
<div class="alert alert-secondary">
<strong>Note</strong> : Une information se cache dans la variable d'environnement <i>FLAG</i>.
</div>
<form action="/" method="post" enctype="multipart/form-data">
<?php
if (isset($success)) {
?>
<div class="alert alert-success">
<?= $success ?>
</div><?php
}
if (isset($error)) {
?>
<div class="alert alert-danger">
<?= $error ?>
</div><?php
}
?>
<h2>Upload file</h2>
<form action="/" method="post" enctype="multipart/form-data">
<div>
<label for="formFile" class="form-label mt-4">Select image to upload</label>
<input class="form-control" type="file" id="formFile" name="file" required />
@ -36,8 +74,18 @@ if(isset($_FILES["file"]))
<div style="margin-top: 10px;">
<button type="submit" class="btn btn-primary">Perform upload</button>
</div>
</form>
</form>
</main>
<h2 style="margin-top: 50px;">Your files</h2>
<ul>
<?php
foreach(scandir(UPLOAD_DIR) as $f) {
if($f === "." or $f === "..") continue;
echo "<li><a href='uploads/".$_SESSION['id']."/$f' target='_blank'>".$f."</a></li>";
}
?>
</ul>
</main>
</body>
</html>

View File

@ -4,7 +4,7 @@ body {
}
.form-signin {
max-width: 530px;
max-width: 800px;
padding: 1rem;
}