看來你需要scandir
而不是水珠,水珠如無法看到UNIX隱藏文件。
<?php
$pid = basename($_GET["prodref"]); //let's sanitize it a bit
$dir = "/assets/$pid/v";
if (is_dir_empty($dir)) {
echo "the folder is empty";
}else{
echo "the folder is NOT empty";
}
function is_dir_empty($dir) {
if (!is_readable($dir)) return NULL;
return (count(scandir($dir)) == 2);
}
?>
請注意,此代碼不是效率的峯會,因爲它不需要讀所有文件只告訴我們,如果目錄爲空。所以,更好的版本將是
function dir_is_empty($dir) {
$handle = opendir($dir);
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
return FALSE;
}
}
return TRUE;
}
順便說一句,不要用話替代布爾值。後者的目的是告訴你是否有空。一個
a === b
表達已返回各自的編程語言,FALSE
或TRUE
方面Empty
或Non Empty
- 所以,你可以使用很結果控制結構像IF()
沒有任何中間值
這只是一個錯字在你的if語句。使用==(比較)而不是單個=(分配)。 –