【发布时间】:2014-05-08 12:18:09
【问题描述】:
我想检查文件夹是否为空。我试过$files!=0 但它不起作用,因为print_r($files); 显示:Array ( [0] => . [1] => .. ) 如何编写正确的条件?
<?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;
?>
【问题讨论】:
-
如果您只对特定文件(扩展名为 .jpg、.gif、.png)感兴趣,您可以使用
glob而不是scandir,如下所述:stackoverflow.com/questions/10591530/…