/ Published in: PHP
Very simple to use.
Calling:
$files = dirContents(DIR_PATH, [FILTER, [TYPE]]);
Examples:
$files = dirContens("my-dir");
$files is array containing all, both files either directories
$files = dirContens("my-dir", "img[0-9]");
$files is array containing files or directories with 'img0', 'img1', 'img2', etc. in their names
$files = dirContents("my-dir/", "\.php", 1);
$files is array containing only files with '.php' in their names
Tip: After calling dirContents() try to call print_r for showing the array of files
echo '<pre>';
print_r($files);
echo '</pre>';
Thank you for your comments.
Expand |
Embed | Plain Text
/* Reading directory contents * @param STRING $dir - Name of the directory * @param STRING $filter - Regular expression * @param BOOLEAN $type - 0 => Files and directories, 1 => Only files, 2 => Only directories * @return ARRAY|BOOLEAN - Returning array if success, else FALSE */ function dirContents($dir, $filter=null, $type=0) { $dir .= "/"; { { if($file!="." && $file!="..") { $arrayOfFiles[] = $file; } } return($arrayOfFiles); }else{return false;} }
Comments
Subscribe to comments
You need to login to post a comment.

thanks!
you should use "sort()" only if the files match the pattern, or else you get an error: if(count($arrayOfFiles)>0){ sort($arrayOfFiles,SORTLOCALESTRING); }