drv16821 2015-06-16 15:39
浏览 47
已采纳

PHP:通过FTP文件夹迭代并创建一个HTML表

I am new to PHP and for the sake of simplicity, I'll say that I am trying to create a crude website in which I want make an HTML table that checks the contents of an FTP folder and either adds a dot to the table if the target is found and an empty cell (w/ a &nbsp) if not.

Here is my code and the folder structure:

<table border=1 >
<?php
foreach ($demolist as &$value) {

    if($value != "." && $value != ".." && $value!=".DS_Store" ){
        ?> <tr> <?php
        echo '<td> <a href="./layouts/'.$value.'/index.html'.'">'.$value.'</a </td>';

        $png = $prezLayouts.$value;

        echo $png.'<br>';

        $sizeList = ftp_nlist($connection, $png);
        natsort($sizeList);

        foreach($sizeList as $target) {

            if($target != "." && 
               $target != ".." && 
               $target !=".DS_Store" && 
               $target != "index.html") {

                echo $target."<br>";

                if ($target == '640.png') { ?> <td> <?php echo "&#8226";  ?> </td> <?php  }

                elseif ($target == '768.png') { ?> <td> <?php echo "&#8226";  ?> </td> <?php }

                elseif ($target == '1000.png') { ?> <td> <?php echo "&#8226";  ?> </td> <?php }

                elseif ($target == '1200.png') { ?> <td> <?php echo "&#8226";  ?> </td> <?php }

                else { ?> <td> <?php echo "&nbsp"; ?> </td> <?php } ?>

                <?php 
            }

        }   
    ?> </tr> <?php
    }
}       
?> </table>

OUTPUT from loop echo's

/domains/~~/html/demo/layouts/about
1000.png
1200.png
/domains/~~/html/demo/layouts/buy
640.png
768.png
/domains/~~/html/demo/layouts/contact
640.png
768.png
1000.png
1200.png
/domains/~~/html/demo/layouts/homepage
640.png
1000.png
1200.png
/domains/~~/html/demo/layouts/social
640.png

Now, my problem is that getting the list of the files in each folder won't let me check for files that don't exist in the folder. (doi) The only crude solution I can think of is creating a static array with [640.png, 768.png, 1000.png, 1200.png] and use that to check against the files in the folders, but I'm positive there is a more elegant solution.

  • 写回答

1条回答 默认 最新

  • drnysdnnb2909701 2015-06-16 17:01
    关注

    Can try the code below. It is a good practice to not break in and out of PHP and HTML if possible.

    <?php
    // Start HTML Table wrapper
    $table = "<table border=1>
    ";
    
    foreach ($demolist as &$value) {
        if($value != "." && $value != ".." && $value != ".DS_Store"){
            // Start Table Row wrapper
            $table .= "<tr>
    ";
            // First Cell
            $table .= "<td><a href='./layouts/$value/index.html'>$value</a></td>";
            $png = $prezLayouts.$value;
            // Second Cell
            table .= "<td>$png</td>";
            // This section does not make sense... your HTML table could result in uneven columns.
            $sizeList = ftp_nlist($connection, $png);
            natsort($sizeList);
    
            foreach($sizeList as $target) {
                // We have already ruled out 3 conditions above, so we only need to rule out index.html
                if($target != "index.html") {
                    $table .= "<td>" . (strripos($target, ".png")?"&#8226;":"&nbsp;") . "</td>";
                }
            }   
        }
        // Close Table Row wrappper
        $table .= "</tr>
    ";
    }
    // Close Table wrapper
    $table .= "</table>
    ";
    echo $table;
    ?>
    

    One improvement I can suggest, if you know the total number of PNG files or all the titles that could be, can use that. Either a for() loop or make an array of the possible file names.

    $imageNames = array("640.png", "768.png", "1000.png", "1200.png");
    $imageExists = array();
    $sizeList = ftp_nlist($connection, $png);
    natsort($sizeList);
    
    foreach($imageNames as $n) {
        foreach($sizeList as $target){
            $imageExists[$n] = true;
        }
    }
    foreach($imageExists as $e){
        $table .= "<td>" . ($e)?"$#8226;":"&nbsp;" . "</td>";
    }
    

    Since we are looking for 4 specific file names, we iterate all possible values, looking for each file name. This will ensure that you have a value for each possible file name. And make your column count the same on each row.

    Maybe better:

    foreach($imageNames as $n) {
        $table .= "<td>" . (array_search($n, $sizeList)?"&#8226;":"&nbsp;") . "</td>"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号