无法检查文件夹是否为空

时间:2014-09-13 14:10:24

标签: 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."/";
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

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
}

等等。可能是什么问题?我在这里缺少什么?

PHP版本是5.5

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

修改

在你的帮助下,这是对所有文件夹说的全局代码&#34;不是空的&#34;:

<?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

如您所见,数组为空。

2 个答案:

答案 0 :(得分:1)

您可以使用glob

它忽略了&#39;。&#39;和&#39; ..&#39;

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

glob仅适用于服务器文件系统上的路径,而不适用于URL。所以如果你想访问你的服务器目录,它可能是这样的:

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

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

答案 1 :(得分:0)

我认为你构建路径的方式不会奏效。请试试这个:

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

}