Directory Image Lister PHP -> XML

Will be useful for an upcoming Flash gallery that dynaically loads images. I didn’t want to reinvent the wheel, but apparently I was too impatient to find the wheel. 😛

PHP Code:

$directory_list = ".";

/*    This may pose a security risk, hence commented out:

if( isset($_GET["dir"])) {
    $directory_list = $_GET["dir"];
} else {
    $directory_list = ".";
}

*/

function getExtension($filename)    {
    $file_ext = substr (strrchr ($filename, "."), 1);
    return ($file_ext);
}
if(is_dir($directory_list))    {

    header('Content-Type: application/xml');    //    Set content type for header
//    header("Content-type: text/xml");    //    Both work, not sure which one's most compatible

    echo "\n\n";
    $dir = opendir($directory_list);
    while(false !== ($file = readdir($dir)))    {
        $type    = filetype($directory_list ."/". $file);
        $ext    = strtolower(getExtension($file));    //    must convert ext to lowercase b/c matching below is case sensitive
        if(($ext == "jpg" || $ext == "png" || $ext == "gif") && $type != "dir") {
            $dimensions    = getimagesize($directory_list ."/". $file);
            $file_width    = $dimensions[0];
            $file_height    = $dimensions[1];
               
            $image_node = <<< EOH
   
        $file
        $file_width
        $file_height
   

EOH;

            echo $image_node;
        }
    }
    closedir($dir);
    echo "";
} else {
    echo "Unable to list \"../" . $directory_list . "\".";
}

Comments are closed.