Checking if a folder is empty or not using PHP


 
Thread Tools Search this Thread
Top Forums Web Development Checking if a folder is empty or not using PHP
# 1  
Old 02-12-2010
Checking if a folder is empty or not using PHP

Hi,
I am sharing this tip with you all.The codes given below will explain
Code:
/**
 * Checking a folder is empty or not.
 * @param string $folderName
 * $folderName should be folder name or path
 * @return TRUE/FALSE (If any file or folder found/Empty folder)
 */
 
function checkFolderIsEmptyOrNot ( $folderName ){
    $files = array ();
    if ( $handle = opendir ( $folderName ) ) {
        while ( false !== ( $file = readdir ( $handle ) ) ) {
            if ( $file != "." && $file != ".." ) {
                $files [] = $file;
            }
        }
        closedir ( $handle );
    }
    return ( count ( $files ) > 0 ) ?  TRUE: FALSE;
}


Last edited by Scott; 02-12-2010 at 06:32 AM.. Reason: Removed self promotion
# 2  
Old 05-01-2010
From the php.net website:
PHP Code:
<?php
public static function isEmptyDir($dir){
     return ((
$files = @scandir($dir)) && count($files) <= 2);
}
?>
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking MD5 Hashes on a folder tree

Hi Guys, I have a backup program that creates incremental backups and generates a MD5 hash at the same time. Each server backup has its own sub folder. Each backup file has a corresponding .md5 file containing a hash and a file name like this. 3410efed13b087322de8036145230a55... (6 Replies)
Discussion started by: Pollardd
6 Replies

2. Shell Programming and Scripting

Checking of file exist in different folder

Hi All, Seeking for your assistance to compare if the file in working directory is found on the DIR2 directory and if not found move the file in DIR2. ex. WORKING_DIR=/home/dir1/ file1.txt DIR2=/home/admin/users file1.txt what i did was found_nonempty='' for file in... (4 Replies)
Discussion started by: znesotomayor
4 Replies

3. Programming

Checking not empty string

I have a string s Are the following equivalent? if ( ! s.empty() ) { } if ( s ) { } (1 Reply)
Discussion started by: kristinu
1 Replies

4. Shell Programming and Scripting

checking csv files with empty fields..!

Hi! I need to learn that how a shell script can transverse a csv file n check if any field is empty or not. means its contains two comma or space b/w commas i.e., "" or " ". can anyone help me out how I can do that.... (10 Replies)
Discussion started by: sukhdip
10 Replies

5. UNIX for Dummies Questions & Answers

Script for checking the files in a folder

Hi , I am using the below script for checking for a file in a folder. if ; then echo 0 else echo 1 fi Is there any way we can check for files which are starting with GL*.csv.What I am trying to do is , I have to check in a folder for the GL*.csv files if there are any files they I... (6 Replies)
Discussion started by: wangkc
6 Replies

6. Shell Programming and Scripting

Sent output to email and empty folder at same time

Hi all, i want to sent output to email and folder at same time. This is my code : echo "Hello" | mailx -s "${SUBJECT}" "${email_add}" >> ${file} I only can sent output to my email but cannot sent to my empty folder....can i know how to done it? (1 Reply)
Discussion started by: proghack
1 Replies

7. Shell Programming and Scripting

The checking of folder existance is not working...

Hi all, I have the following code: if ; then echo 'folder not exist'; else echo 'folder exist'; fi The "testing" folder is not exist in /home/batch , but thhe result is 'folder exist'. It seems that the code cannot detect that the folder "testing" not exist. ANybody know the... (1 Reply)
Discussion started by: suigion
1 Replies

8. Shell Programming and Scripting

Checking if any folder is opened or not?

Alogorithm: ============= Whenever any user open any specific directory: 1) cd /usr/lib The prompt should show an Alert message like 2) echo "You have opened this folder" How should I write shell script this one?.. It should be irrespective of users and... (11 Replies)
Discussion started by: Niroj
11 Replies

9. Shell Programming and Scripting

Checking the Empty line in csv file

Hi all, I have one CSV file(MasterFile.csv) consists of two columns. "./incoming/ABC.CSV","./incoming/ABC_CONTROL.txt" "./incoming/PQR.CSV","./incoming/PQR_CONTROL.txt" "./incoming/123.CSV","./incoming/123_CONTROL.txt" I have written a script to read the MasterFile.csv.Here i want to... (4 Replies)
Discussion started by: sollins
4 Replies

10. UNIX for Dummies Questions & Answers

move files from folder thats are not empty

Hi, I would like to write a shell script that moves files from one folder to another without retrieving the error 'can not find file or folder' when the folder is empty. Any ideas, Thx in advance, Steven. (8 Replies)
Discussion started by: Steven
8 Replies
Login or Register to Ask a Question