0

im trying to echo all jpg image files in a folder. it works, but i dont have the line to show an error that the folder is empty written properly. it shows a php script error instead,

another problem is that i have one .txt file in the directory, this one will read the text for the certain page. the rest of the script will echo the jpg files. it might not echo zero files because of that text file

folder = cat the gategory name/title .txt file = the text for the page .jpg files = are the categories pictures

error is:

Warning: Invalid argument supplied for foreach() in /home/ranshow/domains/show.webking.co.il/public_html/modules/attractions.php on line 67 Gallery is empty, no pics to show 0

<div style="text-align: center;margin-top:25px;margin-bottom: 50px;">
    <?
    $count = "0";

    foreach (glob("attractions/$cat/*.jpg") as $filename) {
    $count++;
    $files[] = $filename;
    $filename = urlencode($filename);
    }

    if($count == "0") { echo "Gallery is empty, no pics to show"; }
    else {
    ?>
    <?
    foreach ($files as $filename) {
    ?>
    <a id="thumb1" href="img.php?img=<?=$filename;?>" class="highslide" onclick="return hs.expand(this)">
    <img src="img.php?img=<?=$filename;?>" width="167" height="150" style="margin: 2px;d0b28c;padding:1px;border: 1px solid #c1c1c1;">
    </a>
    <?
    }
    ?>
    <?
    }
    ?>
    <br />
    <i><?=$count;?> <?=$lang['attractions']['totalimages'];?></i>
    <br />
</div>

how can i fix this? thanks :)

2 Answers2

2

Check count before entering foreach:

$globs = glob("attractions/$cat/*.jpg");
if( $globs ){
    foreach ($globs as $filename) {
        $count++;
        $files[] = $filename;
        $filename = urlencode($filename);
    }
}
Lorenzo Marcon
  • 8,029
  • 5
  • 38
  • 63
0

It seems that you need scandir instead of glob, as glob can't see unix hidden files.

<?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);
}
?>

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

function is_dir_empty($dir) {
  if (!is_readable($dir)) return NULL; 
  $handle = opendir($dir);
  while (false !== ($entry = readdir($handle))) {
    if ($entry != "." && $entry != "..") {
      return FALSE;
    }
  }
  return TRUE;
}

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

a === b

expression already returns Empty or Non Empty in terms of programming language, FALSE or TRUE respectively - so, you can use the very result in control structures like IF() without any intermediate values

Please check this answer of Your Common Sense for more details

Community
  • 1
  • 1
Ghostman
  • 6,042
  • 9
  • 34
  • 53
  • But OP is only checking for `jpg` files, so directory with only `.htaccess` is not empty but still have nothing to display... – dev-null-dweller Jul 13 '13 at 14:46
  • another problem is that i have one .txt file in the directory, this one will read the text for the certain page. the rest of the script will echo the jpg files. folder = cat the gategory name/title .txt file = the text for the page .jpg files = are the categories pictures [code] foreach (glob("attractions/$cat/*.txt") as $textname) { echo nl2br(file_get_contents($textname)); } [/code] this is the line that comes before the pictures script – user2579331 Jul 13 '13 at 14:53