这是我的 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>
但我得到一个奇怪的结果点击这里查看我的页面
我的代码似乎有什么问题?