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

What is the best practice for listing ftp files from a remote server?

1) Using the ftp_connect () function

<?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 print the list of files with print_r ()

<?php

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

$files = scandir($path);

print_r($files);

?>

Both methods output an array with ftp directories / files.

+4
source share
1 answer

My opinion is that the former is preferable why:

  • It clearly states that this is an ftp connection, which means that you can use it not only to write files, but also to download if you need it later, you can also handle errors correctly and provide better error messages or logging
  • allow_url_fopen, - .
+1

All Articles