简体   繁体   English

PHP代码总是告诉我目录不为空

[英]PHP Code always tells me that the directory isn't empty

The code below needs to read the directory uploads/ but he always tells me that the directory is not empty, even when it totally is empty. 下面的代码需要读取目录uploads /,但他总是告诉我该目录不为空,即使该目录完全空。

<?php
  $dir = "uploads/";
  echo (count(glob("$dir/*")) === 0) ? 'Empty' : 'Not empty';
?>

Is there a error in this code or anything or am I just going crazy? 这段代码中是否有错误或任何错误,还是我发疯了?

UPDATED CODE 更新的代码

<?php
  echo (count(glob("uploads/*")) === 0) ? 'Empty' : 'Not empty';
?>

FULL PAGE CODE UPDATE 整页代码更新

<?php
if (array_key_exists('error', $_GET)) {
    echo '<div class="galleryError">That image could not be found, we&#39;re sorry!</div>';
} elseif (array_key_exists('unknownerror', $_GET)) {
    echo '<div class="galleryError">There went something wrong</div>';
} else {
    echo '';
}

if ($handle = opendir('uploads/')) {

    while (false !== ($entry = readdir($handle))) {

        if ($entry != "." && $entry != "..") {

            echo "<div class='imgbox'><a href='fullscreen.php?token=$entry'><img src='$submap$gallery$entry' class='boximg'></a><p class='boxname'>$entry<br /><a href='?view&token=$entry'><small>View this image</small></a></p></div>";
        }
    }

    closedir($handle);
}

// all the above is working but then we have this lonely code over here which refuses to work. //以上所有方法都有效,但随后我们在这里有了这个孤独的代码,它拒绝工作。

echo (count(glob("uploads/*")) == 0) ? 'Empty' : 'Not empty';
?>

glob is silently failing. glob默默地失败了。 I couldn't tell you why, with file system access it's usually permissions related but there could be other factors - it even says in the documentation that the functionality is partially dependent on your server environment... 我无法告诉您为什么,文件系统访问通常与权限相关,但是可能还有其他因素-它甚至在文档中表示功能部分取决于您的服务器环境...

On some systems it is impossible to distinguish between empty match and an error. 在某些系统上,不可能区分空匹配和错误。

When there's a glob error it returns false - and count(false) === 1 (also documented) so it's no surprise folks get into confusing situations when they ignore these checks. 当有一个glob错误则返回false -和count(false) === 1 (也记录),所以一点也不奇怪,人们陷入混乱的状况时,他们忽略了这些检查。 If you really don't care about errors you can utilise the short-hand ternary operator to make an inline false-check, ensuring an array is always passed to count so it will behave as expected: 如果您真的不在乎错误,则可以使用简写三元运算符进行内联错误检查,确保始终传递一个数组以进行count从而使其表现出预期的效果:

$isEmpty = count(glob("uploads/*")?:[]) === 0;

This still doesn't get around the fact that the directory is not being read though - have you tried printing the output of glob when you do have files in the directory? 这仍然不能回避的事实得到该目录没有被阅读,虽然-你试过打印水珠的输出,当必须在目录中的文件? I'd imagine that's borked too. 我想那也很无聊。 What does the following print? 以下内容是什么? :

var_dump(is_readable('./uploads'));

Does the output of the following match your expected working directory? 以下输出是否与您预期的工作目录匹配? :

echo realpath('uploads');

FYI use var_dump when debugging, not print_r as suggested in the comments - it will give you more detail about the variables / types / structures / references etc. that you actually need. FYI在调试时使用var_dump ,而不是注释中建议的print_r它会为您提供有关实际需要的变量/类型/结构/引用等的更多详细信息。


In summary - things to consider: 总结-要考虑的事情:

What OS is running on the server? 服务器上正在运行什么操作系统 What mode is PHP running in? PHP在哪种模式下运行? Is it running under SuPHP / FastCGI or similar context. 它是否在SuPHP / FastCGI或类似环境下运行。 What user owns the ./uploads directory, what permissions are set on it? 什么用户拥有./uploads目录,对其具有什么权限?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM