简体   繁体   中英

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.

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

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


In summary - things to consider:

What OS is running on the server? What mode is PHP running in? Is it running under SuPHP / FastCGI or similar context. What user owns the ./uploads directory, what permissions are set on it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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