2015-04-17 22 views
4

從遠程服務器獲取ftp文件列表的最佳做法是什麼?PHP - 從遠程服務器獲取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:[email protected]"; 

$files = scandir($path); 

print_r($files); 

?> 

兩種方法都輸出一個帶有ftp目錄/文件的數組。

+0

你可以按照第一種方法,因爲這方法 是安全的連接到FTP和掃描 – Prasad

回答

1

我的看法是,第一個是最好的,爲什麼:

  1. 它明確指出,這是FTP連接,這意味着你不僅可以用它來列出文件,但上載太多,如果你需要它以後,你也可以正確地處理錯誤,並提供更好的錯誤信息或記錄
  2. 第二種方法依賴於allow_url_fopen設置,這是更好,因爲可以安全的禁用漏洞
相關問題