SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    function to list files keeps timing out

    Hi guys,

    I have a function which lists all files in a directory and all subdirectories under it. However as my directory contains a lot of files, the function keeps timing out. I have been advised in a previous thread that it is possible to execute the code in blocks. However, not sure how to deploy this solution.

    Any ideas how to stop this function timing out?

    PHP Code:
    function ListFiles($src){
            
    // Get list of all files
            
    $dir opendir($src);
            
            while(
    false !== ($file readdir($dir))) {
                if((
    $file != '.') && ($file != '..')) {
                    if(
    $file == '.svn'){
                        continue;
                    }
                    if(
    is_dir($src '/' $file)) {
                        
    $this->ListFiles($src '/' $file);
                    }
                    else {
                        
    $files[] = $file;
                    }
                } 
            }
            
    closedir($dir);
            
            return 
    $files;
        } 

  2. #2
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,322
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    are you sure it's timing out because of the large number of files? have you tried it in a small number of files situation (but including a folder or two to test the recursion aspect) and did it work ok then?

    where is $files[] being initiated/defined/coming from? (can you start an array like that?: $files[] = whatever; ? i can't remember. i always go $files = array(); first before putting stuff into an array)

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by johnyboy View Post
    are you sure it's timing out because of the large number of files? have you tried it in a small number of files situation (but including a folder or two to test the recursion aspect) and did it work ok then?

    where is $files[] being initiated/defined/coming from? (can you start an array like that?: $files[] = whatever; ? i can't remember. i always go $files = array(); first before putting stuff into an array)
    Hi Johny,

    I actually just tried the function and it's not returning desired results on a small directory. I tried a directory with 1 subdirectory and 4 files listed under the main dir. It only returned the files under the main dir so not sure what's going wrong here...

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    Look into using the RecursiveDirectoryIterator which alleviates the need to manually build an array of all of the file names.
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    Look into using the RecursiveDirectoryIterator which alleviates the need to manually build an array of all of the file names.
    I managed to find a very quick function that recursively lists all files in a directory. However, it only seems to recursively list all files 2 levels deep. Is it possible to modify this code so that it lists all files in all directories?

    PHP Code:
    function get_dir_iterative($dir$exclude = array( '.svn''_dt_backups''.''..' ))
    {
        
    $exclude array_flip($exclude);
        if(!
    is_dir($dir))
        {
            return;
        }
        
        
    $dh opendir($dir);
        
        if(!
    $dh)
        {
            return;
        }

        
    $stack = array($dh);
        
    $level 0;

        while(
    count($stack))
        {
            if(
    false !== ( $file readdir$stack[0] ) ) )
            {
                if(!isset(
    $exclude[$file]))
                {
                    print 
    str_repeat(' '$level 4);
                    if(
    is_dir($dir '/' $file))
                    {
                        
    $dh opendir($dir '/' $file);
                        if(
    $dh)
                        {
                            print 
    "<strong>$file</strong><br />\n";
                            
    array_unshift($stack$dh);
                            
    $level++;
                        }
                    }
                    else 
                    {
                        
                        print 
    "$file<br />\n";
                    }
                }
            }
            else 
            {
                
    closedir(array_shift($stack));
                --
    $level;
            }
        }



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •