PHP 中获取 FTP 文件信息的函数是 ftp_rawlist(),它返回一个包含文件信息的字符串数组。该函数接收两个参数:$ftp_stream(指向 FTP 连接的资源句柄)和 $directory(要列出的目录的路径)。

如何在 PHP 中获取 FTP 文件信息
PHP 提供了 ftp_rawlist() 函数,可用于获取 FTP 目录中的文件信息。它返回一个包含文件信息的字符串数组。
语法:
<code class="php">array ftp_rawlist(resource $ftp_stream, string $directory)</code>
参数:
立即学习“PHP免费学习笔记(深入)”;
-
$ftp_stream:指向 FTP 连接的资源句柄 -
$directory: 要列出的目录的路径
返回值:
一个包含文件信息的字符串数组。每个字符串代表一个文件或目录,格式如下:
-
-rw-r--r-- 1 user group 123456 1999-01-01 00:00 filename(文件) -
drwxr-xr-x 1 user group 123456 1999-01-01 00:00 dirname(目录)
示例:
<code class="php"><?php
// 打开 FTP 连接
$ftp_stream = ftp_connect('example.com');
ftp_login($ftp_stream, 'username', 'password');
// 获取指定目录的文件信息
$files = ftp_rawlist($ftp_stream, '/public_html');
// 输出文件信息
foreach ($files as $file) {
echo $file . PHP_EOL;
}
// 关闭 FTP 连接
ftp_close($ftp_stream);
?></code>输出:
<code>-rw-r--r-- 1 user group 123456 1999-01-01 00:00 index.html drwxr-xr-x 1 user group 123456 1999-01-01 00:00 images -rw-r--r-- 1 user group 123456 1999-01-01 00:00 style.css</code>











