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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据