Example #1
0
function process_directory($path)
{
    $dir = opendir($path);
    while ($file = readdir($dir)) {
        if (substr($file, 0, 1) == '.') {
            continue;
        }
        $filepath = $path . '/' . $file;
        if (is_dir($filepath)) {
            process_directory($filepath);
        } elseif (is_file($filepath)) {
            if (substr($file, -4) == '.php') {
                process_file($filepath);
            }
        } else {
            print "Unknown type: {$filepath}\n";
        }
    }
    closedir($dir);
}
Example #2
0
/**
 * Recursively process a directory, picking regular files and feeding
 * them to process_file().
 *
 * @param string $dir the full path of the directory to process
 * @param string $userfield the prefix_user table field to use to
 *               match picture files to users.
 * @param bool $overwrite overwrite existing picture or not.
 * @param array $results (by reference) accumulated statistics of
 *              users updated and errors.
 *
 * @return nothing
 */
function process_directory($dir, $userfield, $overwrite, &$results)
{
    if (!($handle = opendir($dir))) {
        notify(get_string('uploadpicture_cannotprocessdir', 'admin'));
        return;
    }
    while (false !== ($item = readdir($handle))) {
        if ($item != '.' && $item != '..') {
            if (is_dir($dir . '/' . $item)) {
                process_directory($dir . '/' . $item, $userfield, $overwrite, $results);
            } else {
                if (is_file($dir . '/' . $item)) {
                    $result = process_file($dir . '/' . $item, $userfield, $overwrite);
                    switch ($result) {
                        case PIX_FILE_ERROR:
                            $results['errors']++;
                            break;
                        case PIX_FILE_UPDATED:
                            $results['updated']++;
                            break;
                    }
                }
            }
            // Ignore anything else that is not a directory or a file (e.g.,
            // symbolic links, sockets, pipes, etc.)
        }
    }
    closedir($handle);
}
Example #3
0
function process_directory($path, $fn, $recurse)
{
    $dhandle = opendir($path);
    $files = array();
    if ($dhandle) {
        while (false !== ($fname = readdir($dhandle))) {
            if ($fname != '.' && $fname != '..') {
                $subpath = join_paths($path, $fname);
                if (is_dir($subpath) && $recurse) {
                    // Ignore subdirectories for now.
                    process_directory($subpath, $fn, $recurse);
                } else {
                    $fn($subpath);
                }
            }
        }
        closedir($dhandle);
    }
}
function pleac_Processing_All_Files_in_a_Directory_Recursively()
{
    // Recursive directory traversal function and helper: traverses a directory tree
    // applying a function [and a variable number of accompanying arguments] to each
    // file
    class Accumulator
    {
        public $value;
        public function __construct($start_value)
        {
            $this->value = $start_value;
        }
    }
    // ------------
    function process_directory_($op, $func_args)
    {
        if (is_dir($func_args[0])) {
            $current = $func_args[0];
            foreach (scandir($current) as $entry) {
                if ($entry == '.' || $entry == '..') {
                    continue;
                }
                $func_args[0] = $current . '/' . $entry;
                process_directory_($op, $func_args);
            }
        } else {
            call_user_func_array($op, $func_args);
        }
    }
    function process_directory($op, $dir)
    {
        if (!is_dir($dir)) {
            return FALSE;
        }
        $func_args = array_slice(func_get_args(), 1);
        process_directory_($op, $func_args);
        return TRUE;
    }
    // ----------------------------
    $dirlist = array('/tmp/d1', '/tmp/d2', '/tmp/d3');
    // Do something with each directory in the list
    foreach ($dirlist as $dir) {
        // Delete directory [if empty]     -> rmdir($dir);
        // Make it the 'current directory' -> chdir($dir);
        // Get list of files it contains   -> $filelist = scandir($dir);
        // Get directory metadata          -> $ds = stat($dir);
    }
    // ------------
    $dirlist = array('/tmp/d1', '/tmp/d2', '/tmp/d3');
    function pf($path)
    {
        // ... do something to the file or directory ...
        printf("%s\n", $path);
    }
    // For each directory in the list ...
    foreach ($dirlist as $dir) {
        // Is this a valid directory ?
        if (!is_dir($dir)) {
            printf("%s does not exist\n", $dir);
            continue;
        }
        // Ok, so get all the directory's entries
        $filelist = scandir($dir);
        // An 'empty' directory will contain at least two entries: '..' and '.'
        if (count($filelist) == 2) {
            printf("%s is empty\n", $dir);
            continue;
        }
        // For each file / directory in the directory ...
        foreach ($filelist as $file) {
            // Ignore '..' and '.' entries
            if ($file == '.' || $file == '..') {
                continue;
            }
            // Apply function to process the file / directory
            pf($dir . '/' . $file);
        }
    }
    // ----------------------------
    function accum_filesize($file, $accum)
    {
        is_file($file) && ($accum->value += filesize($file));
    }
    // ------------
    // Verify arguments ...
    $argc == 2 || die("usage: {$argv[0]} dir\n");
    $dir = $argv[1];
    is_dir($dir) || die("{$dir} does not exist / not a directory\n");
    // Collect data [use an object to accumulate results]
    $dirsize = new Accumulator(0);
    process_directory('accum_filesize', $dir, $dirsize);
    // Report results
    printf("%s contains %d bytes\n", $dir, $dirsize->value);
    // ----------------------------
    function biggest_file($file, $accum)
    {
        if (is_file($file)) {
            $fs = filesize($file);
            if ($accum->value[1] < $fs) {
                $accum->value[0] = $file;
                $accum->value[1] = $fs;
            }
        }
    }
    // ------------
    // Verify arguments ...
    $argc == 2 || die("usage: {$argv[0]} dir\n");
    $dir = $argv[1];
    is_dir($dir) || die("{$dir} does not exist / not a directory\n");
    // Collect data [use an object to accumulate results]
    $biggest = new Accumulator(array('', 0));
    process_directory('biggest_file', $dir, $biggest);
    // Report results
    printf("Biggest file is %s containing %d bytes\n", $biggest->value[0], $biggest->value[1]);
    // ----------------------------
    function youngest_file($file, $accum)
    {
        if (is_file($file)) {
            $fct = filectime($file);
            if ($accum->value[1] > $fct) {
                $accum->value[0] = $file;
                $accum->value[1] = $fct;
            }
        }
    }
    // ------------
    // Verify arguments ...
    $argc == 2 || die("usage: {$argv[0]} dir\n");
    $dir = $argv[1];
    is_dir($dir) || die("{$dir} does not exist / not a directory\n");
    // Collect data [use an object to accumulate results]
    $youngest = new Accumulator(array('', 2147483647));
    process_directory('youngest_file', $dir, $youngest);
    // Report results
    printf("Youngest file is %s dating %s\n", $youngest->value[0], date(DATE_ATOM, $youngest->value[1]));
}