简体   繁体   English

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

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

What is the best practice for getting the ftp file list from the remote server? 从远程服务器获取ftp文件列表的最佳实践是什么?

1) Using ftp_connect() function 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);
?>

or 要么

2) Using scandir() and then output the file list with print_r() 2)使用scandir(),然后使用print_r()输出文件列表

<?php

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

$files = scandir($path);

print_r($files);

?>

Both methods output an array with ftp directories/files. 两种方法都输出带有ftp目录/文件的数组。

My opinion is that first one is preferable, why: 我认为第一个是可取的,为什么:

  1. It clearly states that it is ftp connection, which means that you can use it not only for listing files, but for upload too if you need it later, also you can handle errors properly and provide better error messages or logging 它清楚地表明它是ftp连接,这意味着您不仅可以将其用于列出文件,还可以在以后需要时用于上传,还可以正确处理错误并提供更好的错误消息或日志记录
  2. Second method relies on allow_url_fopen settings, which is better to disable because of possible security vulnerabilities 第二种方法依赖allow_url_fopen设置,由于可能存在安全漏洞,最好禁用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM