Simple PHP Image Gallery with Directory Scanning and Price Calculation [migrated]

19 hours ago 1
ARTICLE AD BOX

My goal is to create a dynamic gallery that scans a local directory for images, displays them, and performs some basic logic like counting items and calculating a discounted price.

I've managed to get it working, but as a beginner, I'm sure there are things I'm doing inefficiently or "the old way." I'm looking for feedback on my overall logic and code structure.

Specifically, I'm interested in:

Is using scandir and array_diff the best way to handle local files?

How can I make the image sorting/filtering cleaner?

Are there any security concerns with how I'm handling the GET parameters?

CSS:

* { font-family: "Josefin Sans", sans-serif; background-color: lightcoral; color: black; } header ul { display: flex; justify-content: center; gap: 20px; } li, a { text-decoration: none; list-style: none; } .obrazky { text-align: center; justify-content: center; display: flex; } .active { color: yellowgreen; } .info { color: red; padding: 10px; border-left: 5px solid yellowgreen; font-weight: bold; }

PHP:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Maturitná Práca</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <header> <ul> <?php $zdroj = "./images"; $priečinky = array_diff(scandir($zdroj), ['.', '..']); $volba = $_GET['id'] ?? null; foreach ($priečinky as $kat) { $styl = ($volba == $kat) ? "active" : ""; $meno = ucfirst(str_replace(['-', '_'], ' ', $kat)); echo "<li><a class='$styl' href='?id=$kat'>$meno</a></li>"; } ?> </ul> </header> <?php if ($volba) { $cesta = "images/$volba"; $subory = array_diff(scandir($cesta), ['.', '..']); $pocet = count($subory); function zlava($cena, $percenta) { return $cena - ($cena * ($percenta / 100)); } $nova_cena = zlava(100, 20); echo "<div class='info'>"; echo "Položiek: $pocet "; echo "<br>"; echo "cena: $nova_cena €"; echo "</div>"; foreach ($subory as $foto) { $cista_cesta = "$cesta/$foto"; $nazov = str_replace(['-', '_'], ' ', pathinfo($foto, PATHINFO_FILENAME)); echo "<div style='display:inline-block; margin:10px;'>"; echo "<a href='$cista_cesta' target='_blank'><img src='$cista_cesta' width='100'></a> class"; echo "<br>$nazov"; echo "</div>"; } } ?> </body> </html>
Read Entire Article