动态表行,具体取决于内容的数量

时间:2012-10-18 19:39:43

标签: php ftp

这是我的PHP代码,它连接我与另一个ftp服务器,并向我显示它的内容。然后将结果放在表格中。但是,我希望表在每第三个条目后创建一个新标记。 我确实尝试了以下内容:

<html>
<head><title>some crap</title>
<style type="text/css">
<!--
@import url("style.css");
-->
</style>
</head>
<body>
<table id="gradient-style">
    <thead>
        <tr>
            <th colspan='99'>folders</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <td colspan='99'>End</td>
        </tr>
    </tfoot>
    <tbody>
    <tr>
<td>
<?php
$ftpHost = 'xxx';
$ftpUser = 'xxx';
$ftpPass = 'xxx';
$startDir = 'logs';
$n = 0;

if (isset($_GET['file'])) {
    // Get file contents (can also be fetched with cURL)
    $contents = file_get_contents("ftp://$ftpUser:$ftpPass@$ftpHost/$startDir/" . urldecode($_GET['file']));

    // Get mime type (requires PHP 5.3+)
    $finfo = new finfo(FILEINFO_MIME);
    $mimeType = $finfo->buffer($contents);

    // Set content type header and output file
    header("Content-type: $mimeType");
    echo $contents;
}
else {
    $dir = (isset($_GET['dir'])) ? $_GET['dir'] : '';

    $conn = ftp_connect($ftpHost) or die("Could not connect, please refresh in 2 seconds");
    ftp_login($conn, $ftpUser, $ftpPass);

    // change dir to avoid ftp_rawlist bug for directory names with spaces in them
    ftp_chdir($conn, "$startDir/$dir");

    // fetch the raw list
    $list = ftp_rawlist($conn, '');
    reset($list);

    while (list(, $item) = each($list)) {
        if(!empty($item)) {
            // Split raw result into pieces
            $pieces = preg_split("/[\s]+/", $item, 9);

            // Get item name
            $name = $pieces[8];

            // Skip parent and current dots
            if ($name == '.' || $name == '..' || $name == 'index.html')
                continue;
            if($n%3 == 0){echo "<tr>";}
            // Is directory
            if ($pieces[0]{0} == 'd') {
                echo "<a href='?dir={$dir}/{$name}'><strong>{$name}</strong></a><td>";
            }
            // Is file
            else {
                echo "<a href='displayer.php?file={$dir}/{$name}'>{$name}</a><td>";
            }
            $n++;
            }
        }
    }
    ftp_close($conn);
?>
    </tr>
    </tbody>
</table>
</body>
</html>

但是我得到了一个奇怪的结果click here to see my page
我的代码中出现了什么问题?

1 个答案:

答案 0 :(得分:1)

就我所见,您刚刚获得了一些不正确的标记,请确保您回显的每个结果都有一个开头和一个结束时结束:

// Is directory
            if ($pieces[0]{0} == 'd') {
                echo "<td><a href='?dir={$dir}/{$name}'><strong>{$name}</strong></a></td>";
            }
            // Is file
            else {
                echo "<td><a href='displayer.php?file={$dir}/{$name}'>{$name}</a></td>";
            }