drxvjnx58751 2014-09-13 14:10
浏览 32
已采纳

无法检查文件夹是否为空

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.

  • 写回答

2条回答 默认 最新

  • douhuan1950 2014-09-13 14:38
    关注

    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";
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 图书信息管理系统程序编写
  • ¥15 7-1 jmu-java-m02-使用二维数组存储多元线性方程组
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题