Tag Archives: dynamic

Clean Dynamic Directory Listing with PHP

This here is a quick couple of functions to dynamically display files from any given directory.

It allows you to display it is a nice format with/out the files extension.

Pretty simple, but comments/suggestions/ bugs are very much welcome!!

< ?php

	// DIRECTORY DISPLAY USING GLOB AND A CLEAN PATH OUTPUT.
	// IF ALL PERIMATORS ARE EMPTY WILL SCNA DEFAULT DIRECTORY
	// '$path'  =>  PATH TO DIRECOTRY TO SCAN
	// '$ext'   =>  FILES EXT,  IF YOU WANT OT ECLUDE IT,  OR ONLY SCAN FOR ONE TYPE OF FILE
	// '$start' =>  APPEND TO START OF OUTPUT LINE EG: '
  • ' OR '
    ' // '$end' => APPEND TO END OF OUTPUT LINE EG: '
    \n' OR '
  • \n' => DEFAULT '\n' function direcotryList($path = "./", $ext = NULL, $start = NULL, $end = "\n"){ $output = ""; $ignore = array(".","..","thumbs.db"); foreach(glob($path ."*". $ext) as $file){ if(is_file($file) and !in_array($file, $ignore)){ $output .= $start . buildLink($file, $path, $ext) . $end; } } } // THIS FUNCTION WILL OUTPUT A CLEAN LINK. // BUILT FROM THE PREVIOUS FUNCTION. function buildLink($file, $path, $ext = NULL){ $len1 = strlen($path); $len2 = strlen($file); $start = ($len1 - $len2); $ext = ($start + strlen($ext)); $file = substr($link, $start, -$ext); $name = str_replace(array("_","-"), array(" "," "), $file); $link = "". ucwords($name) .""; return $link; } // EXAMPLES OF USE // OUTPUT SIMPLE LINKS OF ALL FILES IN THE CURRENT DIRECTORY. echo(direcotryList()); // OUTPUT ALL JPG IMAGES IN DIVS $output = "
    \n"; $output .= direcotryList("images/",".jpg","
    ","
    ","\n"); $output .= "
    \n\n"; echo($output); ?>