Php 无法检查文件夹是否为空

Php 无法检查文件夹是否为空,php,Php,我在共享服务器上有一个网站,我想检查某个特定文件夹是否为空。我尝试了至少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."/";

我在共享服务器上有一个网站,我想检查某个特定文件夹是否为空。我尝试了至少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

编辑

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
在你们的帮助下,这是对所有文件夹说“不为空”的glob代码:

正如您看到的,数组是空的。

您可以使用

它忽略了“.”和“…”

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

}

你确定目录中没有应该为空的隐藏文件吗?我没有。但据我所知,前两个代码应该忽略那些文件,正如你所见,我认为它们不会。尝试修改第一个函数,使其返回
$files
数组和
print\r
数组。我也尝试了这个。它总是说文件夹是空的。它怎么能跟这个说呢?如果(计数(全局(“)==0)glob只处理服务器文件系统上的路径,而不是URL。我猜
example.com
实际上是该域的文档根,完整路径类似于
/home/username/public\u html/example.com/Trips/
。这在共享主机上很常见。这可能是他的问题,因为他说他访问
www.example.com/Trips/000019/
。他需要访问文件系统文件夹,而不是url,修复此问题时,其他函数也可能正确。@Schlaus您在路径上是正确的,但此glob函数表示“不为空”“对于所有文件夹,我想知道文件夹中是否有文件。就这些。好了,你的函数已经做到了,除了第二个函数以错误的方式使用了
readdir
(应该是
false!=($file=readdir($dir))
。我很确定他们说目录不是空的,因为它不是空的。我也这么认为。那么我该怎么做才能忽略隐藏的文件/文件夹呢?我的函数会忽略它们。试试看。它总是说文件夹是空的。即使是$server\u dir。
<?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";
    }

}