无法检查文件夹是否为空


Unable to check if folder is empty

我在共享服务器上有一个网站,我想检查特定文件夹是否为空。我试过至少5种方法。我正在测试两个文件夹。清空一个和一个带文件的。

$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/

第一次

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

第二次

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

第三

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
}

等等。问题出在哪里?我在这里错过了什么?

PHP版本为5.5

当我在浏览器中打开www.example.com/Trips/000019/时,它显示我无法访问此文件夹。尽管我可以访问文件夹中的文件,如www.example.com/Trips/000019/a.pdf

编辑

在你们的帮助下,这是对所有文件夹说"不空"的glob代码:

<?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";
}

?>

结果:

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

如您所见,数组为空。

您可以使用glob

它忽略了"。"answers".."

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

glob只使用服务器文件系统上的路径,而不使用URL。因此,如果你想访问你的服务器目录,它可能类似于:

$path = '/var/www/example/dir/*'
if (count(glob($path)) === 0) {
    // empty
}

我认为构建路径的方式行不通。请尝试这个:

<?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";
    }
}