I am using the following script to read a directory. If there is no file in the directory it should say empty. The problem is, it just keeps saying the directory is empty even though there ARE files inside and vice versa.
<?php
$pid = $_GET["prodref"];
$dir = '/assets/'.$pid.'/v';
$q = (count(glob("$dir/*")) === 0) ? 'Empty' : 'Not empty';
if ($q=="Empty")
echo "the folder is empty";
else
echo "the folder is NOT empty";
?>
It seems that you need
scandirinstead of glob, as glob can’t see unix hidden files.Note that this code is not the summit of efficiency, as it’s unnecessary to read all the files only to tell if directory is empty. So, the better version would be
By the way, do not use words to substitute boolean values. The very purpose of the latter is to tell you if something empty or not. An
expression already returns
EmptyorNon Emptyin terms of programming language,falseortruerespectively – so, you can use the very result in control structures likeIF()without any intermediate values