PHP文件列表生成了div框,其中前两个为空

时间:2012-06-25 02:04:39

标签: php html file include

这是我的代码。

前2个div是空的并且乱序,如何用包含的php文件做正确的div框

<html>
    <head></head>
    <body>
    <?php
    $dir    = 'folder/';
    $files = scandir($dir);
    $count=0;
    foreach($files as $file){
        $count++;
        echo '<div style="float:left; margin: 0 0 10px 10px;border:1px solid       #50A4AB;  width:200px"><br>';
        if(strpos($file,".php")){
            include($dir.$file);
        }
        echo '</div><br>';
        if($count==7){echo'<div style="clear:both;"></div><br>';}
    }
    ?>
    </body>
</html>

2 个答案:

答案 0 :(得分:3)

scandir函数返回'。'和'..',你必须从你的结果中排除。这就是为什么前两个div是空的。

您还应该查看the documentation for the scandir function以获取有关订单的信息。

您的HTML格式错误,导致StackOverflow代码突出显示器断开,并且您的缩进不一致。在您寻求帮助时,您应该付出更多努力使您的示例易于阅读。

答案 1 :(得分:0)

  1. @mqsoh所说的一切都是正确的。 (1)
  2. 您的代码存在巨大的安全风险。自动将文件夹中的所有php文件加载为包含是危险的。您应该只加载已知的命名文件。
  3. 我已编辑了您的问题,以便代码更具相关性。
  4. 您应该在以下内容中包含if语句:
  5. if ($file != "." && $file !="..") {
    // Do stuff here
    }

    在您的代码中,如下:

    foreach($files as $file){
        if ($file != "." && $file !="..") {
            $count++;
            echo '<div style="float:left; margin: 0 0 10px 10px;border:1px solid       #50A4AB;  width:200px"><br>';
            if(strpos($file,".php")){
                // This is a big security vulnerability
                include($dir.$file);
            }
            echo '</div><br>';
            if($count==7){echo'<div style="clear:both;"></div><br>';}
        }
    }