【问题标题】:if folder is empty echo message, if not show all jpg files如果文件夹为空,则回显消息,如果不显示所有 jpg 文件
【发布时间】:2013-07-11 22:52:20
【问题描述】:

我正在尝试回显文件夹中的所有 jpg 图像文件。 它可以工作,但我没有显示正确写入文件夹为空的错误的行。它显示了一个 php 脚本错误,

另一个问题是我在目录中有一个 .txt 文件,这个 将阅读特定页面的文本。脚本的其余部分 将回显 jpg 文件。因此它可能不会回显零文件 文本文件

文件夹 = cat 网关名称/标题 .txt 文件 = 页面文本 .jpg 文件 = 是分类图片

错误是:

警告:在第 67 行的 /home/ranshow/domains/show.webking.co.il/public_html/modules/attractions.php 中为 foreach() 提供的参数无效 画廊是空的,没有照片可显示 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>

我该如何解决这个问题?谢谢:)

【问题讨论】:

    标签: php html


    【解决方案1】:

    进入foreach前检查count:

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

    【讨论】:

    • count on false 仍然返回 1,并且 glob 可以在错误时返回 false。
    • 正确点@dev-null-dweller,我删除了count,现在应该没问题了
    【解决方案2】:

    看来您需要scandir 而不是 glob,因为 glob 看不到 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 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;
    }
    

    顺便说一句,不要使用 words 代替 boolean 值。后者的目的是告诉您某物是否为空。一个

    a === b
    

    就编程语言而言,表达式已经分别返回EmptyNon EmptyFALSETRUE - 因此,您可以在IF() 等控制结构中使用结果,而无需任何中间值

    请查看Your Common Sense的这个答案了解更多详情

    【讨论】:

    • 但是 OP 只检查 jpg 文件,所以只有 .htaccess 的目录不是空的,但仍然没有任何内容可显示...
    • 另一个问题是我在目录中有一个 .txt 文件,这个文件会读取特定页面的文本。脚本的其余部分将回显 jpg 文件。文件夹 = cat 门类名称/标题 .txt 文件 = 页面的文本 .jpg 文件 = 是类别图片 [代码] foreach (glob("attractions/$cat/*.txt") as $textname) { echo nl2br (file_get_contents($textname)); } [/code] 这是图片脚本之前的那一行
    猜你喜欢
    • 2018-01-02
    • 2013-10-18
    • 2017-09-28
    • 1970-01-01
    • 2015-12-20
    • 2018-05-14
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    相关资源
    最近更新 更多