SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    detect empty folder

    Hello all,

    I'm not really sure how to use this custom function.

    I want to detect whether a folder is empty or not... can someone point me in the wrong direction.

    Code PHP:
     
    $folder = "pics/x";
     
    function check_empty_folder ( $folder )
    {
    	$files = array ();
    	if ( $handle = opendir ( $folder ) ) {
    		while ( false !== ( $file = readdir ( $handle ) ) ) {
    			if ( $file != "." && $file != ".." ) {
    				$files [] = $file;
    			}
    		}
    		closedir ( $handle );
    	}
    	return ( count ( $files ) > 0 ) ? FALSE : TRUE;
    }
     
     
     
    if (check_empty_folder==0) {
    echo "true";
    }
    else {
    echo "false";
    }

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    $folder "pics/x" ;

    if( 
    check_empty_folder$folder) === true ) {

    }

    //or to achieve what you have there:
    echo check_empty_folder$folder ) ;  
    // will echo true or false 
    What happens if you forget to add a $folder though?

  3. #3
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks that helped. I feel much better now.

  4. #4
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can speed up that code drasitically if all you care about is empty vs not-empty:
    PHP Code:
    function check_empty_folder $folder )
    {
        if ( 
    $handle opendir $folder ) ) {
            while ( 
    false !== ( $file readdir $handle ) ) ) {
                if ( 
    $file != "." && $file != ".." ) {
                    return 
    false;
                }
            }
            
    closedir $handle );
        }
        return 
    true;

    There's no point in going through all of the files; just return false after the first file is found.
    MySQL v5.1.58
    PHP v5.3.6

  5. #5
    SitePoint Enthusiast freezea's Avatar
    Join Date
    Apr 2009
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    function check_empty_folder ( $folder )
    {
    $files = array ();
    if ( $handle = opendir ( $folder ) ) {
    while ( false !== ( $file = readdir ( $handle ) ) ) {
    if ( $file != "." && $file != ".." ) {
    $files [] = $file;
    }
    }
    closedir ( $handle );
    }
    return ( count ( $files ) > 0 ) ? FALSE : TRUE;
    }
    RAQ Report: Web-based Excel-like Java reporting tool.

  6. #6
    SitePoint Evangelist Dave Morton's Avatar
    Join Date
    Sep 2003
    Location
    Carson City, NV
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tested this on a Win32 system, and it seems to work for both the presence of directories and files:

    PHP Code:

    <?php
     
    function checkEmptyFolder($baseDir$folder) {
       if (!
    is_dir("$baseDir/$folder")) return false;
       
    $test glob("$baseDir$folder/*");
       
    //var_dump($test);
       
    $out = (empty($test)) ? false true;
     }
    $baseDir "./";
    $folder "empty";
    $x checkEmptyFolder($baseDir$folder);
    ?>
    I don't think you can get a whole lot simpler than that. Of course, whether it works on other OS platforms, I just don't know, but I'm sure it would.
    Making a difference, one little psychotic episode at a time
    Geek Cave Creations
    Beta testers needed for pChat
    Dave's Gallery


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
  •