ls() - Return Folder Contents

The ls() function will take a pattern and a folder as the argument and go through it(recursively if needed)and return the list of all files in that folder.

Arguments

$pattern
The first argument is the pattern(or the file mask) to look out for. Example: ls("*.html"). This supports any pattern supported by the glob function. This is an optional argument - if nothing is given, it defaults to "*". Some possible values...
*Matches everything.
*.phpAll files with the extension php
*.{php,html}Files with extension 'php' or 'html'
file[1-3].phpIt could match file1.php, file2.php and file3.php
$folder
The path of the directory of which directory list you want. This is an optional argument. If empty, the function will assume the value to be the current folder.
$recursively
The function will traverse the folder tree recursively if this is true. Defaults to false.
$options
An array of values 'return_files' or 'return_folders' or both. This decides what must be returned.

Returned Value

A flat array with the path of all the files(no folders) that matches the condition given. An example is...


Array
(
    [0] => 1.php
    [1] => 2.php
    [2] => 3.txt
    [3] => 4.txt
    [4] => 5 5.txt
    [5] => 6/
    [6] => 6/8.css
    [7] => 6/9.html
    [8] => 7/
    [9] => 7/10.net.txt
    [10] => 7/11.php
)

Code


<?php
/**
 * This funtion will take a pattern and a folder as the argument and go thru it(recursivly if needed)and return the list of 
 *               all files in that folder.
 * Link             : http://www.bin-co.com/php/scripts/filesystem/ls/
 * Arguments     :  $pattern - The pattern to look out for [OPTIONAL]
 *                    $folder - The path of the directory of which's directory list you want [OPTIONAL]
 *                    $recursivly - The funtion will traverse the folder tree recursivly if this is true. Defaults to false. [OPTIONAL]
 *                    $options - An array of values 'return_files' or 'return_folders' or both
 * Returns       : A flat list with the path of all the files(no folders) that matches the condition given.
 */
function ls($pattern="*"$folder=""$recursivly=false$options=array('return_files','return_folders')) {
    if(
$folder) {
        
$current_folder realpath('.');
        if(
in_array('quiet'$options)) { // If quiet is on, we will suppress the 'no such folder' error
            
if(!file_exists($folder)) return array();
        }
        
        if(!
chdir($folder)) return array();
    }
    
    
    
$get_files    in_array('return_files'$options);
    
$get_foldersin_array('return_folders'$options);
    
$both = array();
    
$folders = array();
    
    
// Get the all files and folders in the given directory.
    
if($get_files$both glob($patternGLOB_BRACE GLOB_MARK);
    if(
$recursivly or $get_folders$folders glob("*"GLOB_ONLYDIR GLOB_MARK);
    
    
//If a pattern is specified, make sure even the folders match that pattern.
    
$matching_folders = array();
    if(
$pattern !== '*'$matching_folders glob($patternGLOB_ONLYDIR GLOB_MARK);
    
    
//Get just the files by removing the folders from the list of all files.
    
$all array_values(array_diff($both,$folders));
        
    if(
$recursivly or $get_folders) {
        foreach (
$folders as $this_folder) {
            if(
$get_folders) {
                
//If a pattern is specified, make sure even the folders match that pattern.
                
if($pattern !== '*') {
                    if(
in_array($this_folder$matching_folders)) array_push($all$this_folder);
                }
                else 
array_push($all$this_folder);
            }
            
            if(
$recursivly) {
                
// Continue calling this function for all the folders
                
$deep_items ls($pattern$this_folder$recursivly$options); # :RECURSION:
                
foreach ($deep_items as $item) {
                    
array_push($all$this_folder $item);
                }
            }
        }
    }
    
    if(
$folderchdir($current_folder);
    return 
$all;
}

blog comments powered by Disqus
Subscribe to Feed