Ideally the directory only contains images to create a photo gallery.
The directory is chosen by name (which is read from get-variable) eg: www.mysite.com/album?id="xyz"
As it is now if i give in the variable "../" it shows the wrong directory.
Checking if the variable starts with a "." is not a good solution.
How can I sanitize my input in php to prevent a directory traversal attack and how do I make sure only images are listed?
- Code: Select all
<?php
if(isset($_GET['album']) && $_GET['album'] != null) {
if(!preg_match("/^[.]/",$_GET['album'])){
$directory="<path_to_dir>".$_GET['album']."/";
foreach(glob($directory.'*') as $filename){
echo"<tr><td><img style='width:400px;' src='".$directory.basename($filename)."' ></td></tr>";
}
}
}
?>




