Fix error when no file have been uploaded yet

This commit is contained in:
Pierre HUBERT 2025-01-31 11:29:33 +01:00
parent 20a7eeb659
commit b63e2b61ec
2 changed files with 16 additions and 11 deletions

View File

@ -4,5 +4,5 @@ You need to set the `FLAG` environment variable for this challenge to work!
## Run the image
```bash
docker run --rm --name unsafe_login --env FLAG='FLAG{UNSAFEGALLERY}' -p 3565:80 -it pierre42100/gns3-appliance-unsafe-gallery
docker run --rm --name unsafe_login --env FLAG='FLAG{UNSAFEGALLERY}' -p 3568:80 -it pierre42100/gns3-appliance-unsafe-gallery
```

View File

@ -10,13 +10,12 @@ if (!isset($_SESSION["id"]))
define('UPLOAD_DIR', __DIR__ . "/uploads/" . $_SESSION["id"]);
if (isset($_FILES["file"])) {
$dest_file_name = (string)time() ."-". str_replace("/", "", $_FILES["file"]["name"]);
$dest_file_name = (string) time() . "-" . str_replace("/", "", $_FILES["file"]["name"]);
// Create target directory
if(!is_dir(UPLOAD_DIR) && !mkdir(UPLOAD_DIR, 0770, true)) {
if (!is_dir(UPLOAD_DIR) && !mkdir(UPLOAD_DIR, 0770, true)) {
$error = "Failed to create storage directory!";
}
else if ($_FILES["file"]["size"] > 10000) {
} 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!";
@ -76,15 +75,21 @@ if (isset($_FILES["file"])) {
</div>
</form>
<?php
if (is_dir(UPLOAD_DIR)) {
?>
<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>";
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><?php
}
?>
</ul>
</main>
</body>