在webroot外部的文件夹和子文件夹中显示图像

I'm using this code successfully in image.php to get an image outside the webroot:

<?

header('Content-Type: image/png');
readfile('/www/htdocs/w01238e7/scans/10001/2016-10-22/162456.png');

?>

I implement this php file in my main page and show the image:

/image.php?file=myfile.png

But I have several other folders incl. subfolders with different filenames (jpg, png or pdf). What I need is a function which scans a main folder, e.g.

/www/htdocs/w01238e7/scans/'

and identifies any subfolder and returns the files founds automaticly in image.php.

What I have tried so far (but doesn't shows the images):

<?

header('Content-Type: image/png');

$folder = "/www/htdocs/w01238e7/scans/10001";

$allfiles = scandir($folder);

foreach ($allfiles as $file) {
$fileinfo = pathinfo($folder."/".$file); 
$size = ceil(filesize($folder."/".$file)/1024); 
if ($datei != "." && $file != ".."  && $file != "_notes" &&
$bildinfo['basename'] != "Thumbs.db") { 

$imagetype= array("jpg", "jpeg", "gif", "png");

if(in_array($fileinfo['extension'],$imagetype)) {
?>

<div class="galerie">
    <a href="<?php echo $fileinfo['dirname']."/".$fileinfo['basename'];?>">
    <img src="<?php echo $fileinfo['dirname']."/".$fileinfo['basename'];?>" width="140" alt="Vorschau" /></a>
    <span><?php echo $fileinfo['filename']; ?> (<?php echo $size ; ?>kb)</span
</div>

<?
} else { 
?>

<div class="file">
    <a href="<?php echo $fileinfo['dirname']."/".$fileinfo['basename'];?>">&raquo <?php echo $fileinfo['filename']; ?></a>
(<?php echo $fileinfo['extension']; ?> | <?php echo $size ; ?>kb)
</div>

<? } ?>

<? }; }; ?>