PHP - 从远程服务器获取 ftp 列表的最佳做法是什么?

PHP - What is the best practice for getting the ftp list from the remote server?

从远程服务器获取 ftp 文件列表的最佳做法是什么?

1) 使用 ftp_connect() 函数

<?php

// set up connection
$conn = ftp_connect($ftp_server);

// login with username and password
$login = ftp_login($conn, $ftp_user_name, $ftp_user_pass);

// get contents of the current directory
$content = ftp_nlist($conn, ".");

// output content
var_dump($content);
?>

2) 使用 scandir() 然后用 print_r()

输出文件列表
<?php

$path = "ftp://login:password@ftpserver";

$files = scandir($path);

print_r($files);

?>

两种方法都输出一个数组 ftp directories/files.

我的意见是第一个更好,为什么:

  1. 明明是ftp连接,也就是说你不仅可以用它来列出文件,以后需要的话也可以用来上传,也可以妥善处理错误,提供更好的服务错误消息或日志记录
  2. 第二种方法依赖于allow_url_fopen设置,由于可能存在安全漏洞,最好禁用