Return to Snippet

Revision: 30127
at August 9, 2010 07:57 by sandman7OR


Updated Code
<?php 
$root = $_SERVER["DOCUMENT_ROOT"];
$path = "/NCCDC/test/docs"; 

$allowed = array('jpg','gif','png'); /* file extension that you want to keep */ 

$iterator = new DirectoryIterator($root.$path);
/* See php documentation on iterator */
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isFile()) {
         $path = $fileinfo->getPathname();
         $filename = $fileinfo->getFilename(); /*filename like 'picture1.jpg' */
         $info = pathinfo($filename); 
         $ext =  $info['extension']; /* extension like 'jpg' */
         $filename_noext =  basename($filename,'.'.$ext);/*filename without extension like 'picture1' */
         /* Test to see if extension is allowed */
         if (in_array($ext, $allowed)){
            echo "<a href='$path'>$filename_noext</a><br/> \n";
         }
    }
}
?>

Revision: 30126
at August 9, 2010 07:38 by sandman7OR


Updated Code
<?php 
$root = $_SERVER["DOCUMENT_ROOT"];
$path = "/test/docs";

$iterator = new DirectoryIterator($root.$path);
/* See php documentation on iterator */
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isFile()) {
         $path = $fileinfo->getPathname();
         $filename = $fileinfo->getFilename(); /*filename like 'picture1.jpg' */
         $info = pathinfo($filename); 
         $filename_noext =  basename($filename,'.'.$info['extension']);/*filename without extension like 'picture1' */
         echo "<a href='$path'>$filename</a><br/>";
    }

}

?>

Revision: 30125
at August 8, 2010 07:59 by sandman7OR


Initial Code
$root = $_SERVER["DOCUMENT_ROOT"];
$path = "/gc/docs/";
if ($handle = opendir($root.$path)) {
    $allFiles = array();  
    /* loop over the directory. */
    while (false !== ($file = readdir($handle))) {
         if (strpos($file, '.m4a',1)||strpos($file, '.jpg',1) ) { 
            //add to array
            $allFiles[] = $file;
         }
    }
    closedir($handle);
}
sort( $allFiles);
foreach($allFiles as $row){
      echo "<p><a href='/gc/docs/$row'>$row</a> </p>";
}

?>

Initial URL

                                

Initial Description
Loops through all files of a certain extension in the specified directory.  Can be used to list all images in a given directory.

Initial Title
Load Folder into PHP array

Initial Tags
files, directory

Initial Language
PHP