erdomester
erdomester

Reputation: 11829

Unable to check if folder is empty

I have a website on a shared server and I want to check if a specific folder is empty. I tried out at least 5 ways. I am testing two folders. And empty one and one with a file.

$userid = '000019'; //000019 is empty, 000021 has one file
$server_dir = $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$dir = $server_dir . "/Trips/".$userid."/";
echo 'DIR: '.$dir; //www.example.com/Trips/000019/

FIRST

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; } 
if (checkFolderIsEmptyOrNot($dir)) { 
echo 'X'; 
} else { 
echo 'Y'; 
} 
echo 'EMPTY?: '.checkFolderIsEmptyOrNot($dir); //always Y

SECOND

function dir_is_empty($path)
{
    $empty = true;
    $dir = opendir($path); 
    while($file = readdir($dir)) 
    {
        if($file != '.' && $file != '..')
        {
            $empty = false;
            break;
        }
    }
    closedir($dir);
    return $empty;
}

echo 'EMPTY?: '.dir_is_empty($dir).'<br>'; //always 1

THIRD

function is_dir_empty($dir) {
  if (!is_readable($dir)) return NULL; 
  return (count(scandir($dir)) == 2);
}

if (is_dir_empty($dir)) {
  echo "the folder is empty"; 
}else{
  echo "the folder is NOT empty";  //always NOT empty
}

And so on. What can be the problem? What am I missing here?

PHP version is 5.5

When I open www.example.com/Trips/000019/ in the browser it says I don't have access to this folder. Although I can access the file inside the folder, like www.example.com/Trips/000019/a.pdf

EDIT

With your help guys this is the glob code that says to all folder "not empty":

<?php
header('Content-type: text/html; charset=UTF-8');
session_start();

echo $_SESSION['userid'] = '000019';
$server_dir = $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
//$dir = $server_dir . "/Trips/".$userid."/";
$dir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "Trips"  . DIRECTORY_SEPARATOR .  $_SESSION['userid']  . DIRECTORY_SEPARATOR;

echo 'DIR: '.$dir.'<br>';

$files = array();
if ($dh = opendir($dir)) {
    while (false !== ($file = readdir($dh))) {
        if ($file != "." && $file != "..") $files[] = $file;
    }
}
print_r($files);

if (count(glob($dir)) === 0 ) { 
    echo "the folder is empty"; 
} else {
    echo "the folder is NOT empty";
}



?>

Result:

000019
DIR: /home/myusername/public_html/Trips/000019/
Array ( ) the folder is NOT empty

As you see the Array is empty.

Upvotes: 0

Views: 394

Answers (2)

Schlaus
Schlaus

Reputation: 19212

I think the way you are constructing the path wont work. Please try with this:

<?php
$userid = '000019';

$dir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "Trips"  . DIRECTORY_SEPARATOR .  $userid  . DIRECTORY_SEPARATOR;

function dir_is_empty($path, $hiddenFilesCountAsFiles = true)
{
    $empty = true;
    if (!is_dir($path)) throw new Exception("$path is not a valid path");
    if (!is_readable($path)) throw new Exception("$path is not readable");

    if ($dir = opendir($path)) {
        while(false !== ($file = readdir($dir))) {
            if($hiddenFilesCountAsFiles && strpos($file, ".") !== 0) {
                $empty = false;
                break;
            } elseif (!$hiddenFilesCountAsFiles && $file != "." && $file != "..") {
                $empty = false;
                break;
            }
        }
        closedir($dir);
    } else {
        throw new Exception("Could not open $path");
    }
    return $empty;
}

$empty = (dir_is_empty($dir)) ? "true" : "false";
echo "Path $dir is empty: $empty";

if ($empty === "false") {
    echo "Directory contents:<br>";
    if ($dir = opendir($path)) {
        while(false !== ($file = readdir($dir))) {
            if ($file != "." && $file != "..") {
                echo "$file<br>";
            }
        }
        closedir($dir);
    } else {
        echo "Could not open directory";
    }

}

Upvotes: 0

Lucas Vieira
Lucas Vieira

Reputation: 131

You can use glob

It ignores '.' and '..'

if (count(glob("path/*")) === 0 ) { // empty

glob only works with paths on the server's file system, not URLs. so if you want to access your server dir it may be something like:

$path = '/var/www/example/dir/*'

if (count(glob($path)) === 0) {
    // empty
}

Upvotes: 1

Related Questions