Search code examples
phpdirectoryscandir

How to check,that there's nothing in the folder?


I would like to check if the folder is empty or not. I tried $files!=0 but it doesn't work,because the print_r($files); shows this: Array ( [0] => . [1] => .. ) How to write the right condition?

<?php
$folder = "images/thumbs/";
$files  = scandir($folder);

if ("the folder is not empty") {
    $output = "<div>";
    foreach ($files as $file) {
        if (substr($fajl, -4) == ".jpg" || substr($fajl, -4) == ".png" || substr($fajl, -4) == ".gif") {
            $output .= "<img src=\"{$folder}{$file}\" alt=\"\">";
        }
    }
    $output .= "</div>";
} else {
    $output = "<p>There are no thumbnails in the folder.</p>";

}
return $output;
?>

Solution

  • You can count the items in the array

    if (count($files) > 2)