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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么