程序演示 http://labs.phpz.org/ftplist/
源代码如下:
<?php
$info = array (
'user' => 'username',
'pass' => 'password',
'host' => 'ftp host',
'port' => '21',
'path' => '',
'pasv' => true,
'level' => 3,
'charset' => 'gb2312',
);
ftplist($info);
function ftplist($info) {
set_time_limit(0);
$ftp_conn = @ ftp_connect($info['host'], intval($info['port']), 5) or die('ERR: 链接服务器失败!');
echo "INFO: 成功连接服务器./n";
@ ftp_login($ftp_conn, $info['user'], $info['pass']) or die("ERR: 登录失败!");
echo "INFO: 登录成功./n";
if (ftp_pasv($ftp_conn, $info['pasv'])) {
printf("INFO: %s 被动模式./n", $info['pasv'] ? '开启' : '关闭');
} else {
echo "ERR: 模式设置失败./n";
}
fpath($info['path']);
printf("/nftp://%s:%s@%s:%s%s/n/n", $info['user'], $info['pass'], $info['host'], $info['port'], $info['path']);
get_file_list($ftp_conn, $info['path'], 0, intval($info['level']), $info['charset']);
ftp_close($ftp_conn);
}
function get_file_list($ftp, $path, $level, $max_level = 3, $in_char = 'utf-8') {
if ($level >= $max_level) {
return;
}
$list = ftp_rawlist($ftp, $path);
for ($i = 0; $i < count($list); $i++) {
$finfo = file_info($list[$i], $path);
fchar($finfo['info'], $finfo['file'], $in_char);
flush();
if ($finfo['isdir']) {
get_file_list($ftp, $finfo['file'], $level +1, $max_level, $in_char);
}
}
}
function file_info($fileinfo, $path) {
$isdir = strtolower($fileinfo[0]) == 'd';
$info = substr($fileinfo, 0, 56);
$file = $path . substr($fileinfo, 56);
$isdir && fpath($file);
return array (
'isdir' => $isdir,
'info' => $info,
'file' => $file
);
}
function fchar($info, $file, $in_char) {
if ($in_char != 'utf-8') {
$file = iconv($in_char, 'utf-8', $file);
}
echo "$info$file /n";
}
function fpath(& $path) {
substr($path, -1) != '/' && $path .= '/';
$path[0] != '/' && $path = '/' . $path;
}
?>