Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Directory Contents
#1
Credits and Source: http://www.dreamincode.net/

Name: Directory Contents
Description: This snippet takes a directory and returns or excludes certain files/directories from the output.
Snippet:
PHP Code:
<?php
define
('FILE_FLAG'1);
define('DIR_FLAG'2);

function 
getArtefacts($searchDir$ear = array(), $invert false$type 3){
    
$far = array();
    if(
$handle opendir($searchDir)){
        while(
false !== ($file readdir($handle))){
            
$temp false;
            foreach(
$ear as $value){
                
$temp |= preg_match('/^' $value '$/iD'$file);
                if(
$temp != false) break;
            }
            if(
$invert)    $temp = !$temp;
            
$temp |= preg_match('/^\.{1,2}$/iD'$file);
            if(!
$temp){
                switch(
$type){
                    default:
                    
array_push($far$file);
                    break;
                    case 
2:
                    if(
is_dir($searchDir '/' $file)) array_push($far$file);
                    break;
                    case 
1:
                    if(
is_file($searchDir '/' $file)) array_push($far$file);
                    break;
                }
            }
        }
        
closedir($handle);
    }
    return 
$far;
}
?>

Instructions: string $dir: directory you wish to search
array $ear: an array of regular expressions with what you wish to include or exclude from the results
boolean $invert: whether you wish to include or exclude TRUE = include, FALSE = exclude
integer $type: a flag setting which will include files or directories inclusively or exclusively, FILE_FLAG for files, DIR_FLAG for directories, and FILE_FLAG | DIR_FLAG for both
RETURNS
array $far: an array with files/directories

Examples:
// prints all the folders in the directory
print_r('.', getArtefacts(array('.*\.[a-z0-9]{3,4}')));
print_r(getArtefacts('.', array(), false, DIR_FLAG));

// prints all the files in the directory
print_r('.', getArtefacts(array('.*\.[a-z0-9]{3,4}'), true));
print_r(getArtefacts('.', array(), false, FILE_FLAG));

// prints all .htm or .html files in the directory
print_r('.', getArtefacts(array('.*\.html*'), true));

// prints all files that don't end in .htm or .html
print_r('.', getArtefacts(array('.*\.html*'), false, FILE_FLAG));

Thankyou for reading Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP echo contents of folder help DAMINK™ 3 1,076 06-01-2012, 04:07 PM
Last Post: DAMINK™
  Submit Textfile Contents To Database xbiohazardx 13 3,330 12-06-2010, 01:28 AM
Last Post: Chimi
  Delete directory and all content zone 0 601 11-08-2009, 08:50 AM
Last Post: zone
  Directory Contents zone 0 528 11-07-2009, 03:38 AM
Last Post: zone
  Count the Number of Files in a Directory zone 0 560 11-06-2009, 06:44 AM
Last Post: zone

Forum Jump:


Users browsing this thread: 1 Guest(s)