PHP 日本研討會 2024

ftp_mlsd

(PHP 7 >= 7.2.0, PHP 8)

ftp_mlsd傳回指定目錄中的檔案列表

描述

ftp_mlsd(FTP\Connection $ftp, string $directory): array|false

參數

ftp

一個 FTP\Connection 的實例。

directory

要列出的目錄。

傳回值

成功時傳回指定目錄中檔案資訊的陣列,失敗時傳回 false

更新日誌

版本 描述
8.1.0 ftp 參數現在需要一個 FTP\Connection 的實例;先前,需要一個 resource

範例

範例 1 ftp_mlsd() 範例

<?php

// 設定基本連線
$ftp = ftp_connect($ftp_server);

// 使用使用者名稱和密碼登入
$login_result = ftp_login($ftp, $ftp_user_name, $ftp_user_pass);

// 取得目前目錄的內容
$contents = ftp_mlsd($ftp, ".");

// 輸出 $contents
var_dump($contents);

?>

以上範例會輸出類似如下的內容

array(5) {
  [0]=>
  array(8) {
    ["name"]=>
    string(1) "."
    ["modify"]=>
    string(14) "20171212154511"
    ["perm"]=>
    string(7) "flcdmpe"
    ["type"]=>
    string(4) "cdir"
    ["unique"]=>
    string(11) "811U5740002"
    ["UNIX.group"]=>
    string(2) "33"
    ["UNIX.mode"]=>
    string(4) "0755"
    ["UNIX.owner"]=>
    string(2) "33"
  }
  [1]=>
  array(8) {
    ["name"]=>
    string(2) ".."
    ["modify"]=>
    string(14) "20171212154511"
    ["perm"]=>
    string(7) "flcdmpe"
    ["type"]=>
    string(4) "pdir"
    ["unique"]=>
    string(11) "811U5740002"
    ["UNIX.group"]=>
    string(2) "33"
    ["UNIX.mode"]=>
    string(4) "0755"
    ["UNIX.owner"]=>
    string(2) "33"
  }
  [2]=>
  array(8) {
    ["name"]=>
    string(11) "public_html"
    ["modify"]=>
    string(14) "20171211171525"
    ["perm"]=>
    string(7) "flcdmpe"
    ["type"]=>
    string(3) "dir"
    ["unique"]=>
    string(11) "811U5740525"
    ["UNIX.group"]=>
    string(2) "33"
    ["UNIX.mode"]=>
    string(4) "0755"
    ["UNIX.owner"]=>
    string(2) "33"
  }
  [3]=>
  array(8) {
    ["name"]=>
    string(10) "public_ftp"
    ["modify"]=>
    string(14) "20171211174536"
    ["perm"]=>
    string(7) "flcdmpe"
    ["type"]=>
    string(3) "dir"
    ["unique"]=>
    string(11) "811U57405EE"
    ["UNIX.group"]=>
    string(2) "33"
    ["UNIX.mode"]=>
    string(4) "0755"
    ["UNIX.owner"]=>
    string(2) "33"
  }
  [4]=>
  array(8) {
    ["name"]=>
    string(3) "www"
    ["modify"]=>
    string(14) "www"
    ["perm"]=>
    string(7) "flcdmpe"
    ["type"]=>
    string(3) "dir"
    ["unique"]=>
    string(11) "811U5740780"
    ["UNIX.group"]=>
    string(2) "33"
    ["UNIX.mode"]=>
    string(4) "0755"
    ["UNIX.owner"]=>
    string(2) "33"
  }
}

參見

新增註解

使用者貢獻的註解 3 個註解

fantastory dot net at gmail dot com
3 年前
從腳本執行時,此函式可能需要 ftp_pasv,以將伺服器切換至被動模式。

如果您位於防火牆後方,ftp_mlsd 將傳回 FALSE,否則會傳回其他值。
andy at sarver dot pro
1 年前
我還沒有找到這個問題的解決方法;但我注意到 ftp_mlsd 指令一次可以抓取的檔案數量有限。對我來說,它介於 7,500 到 8,500 個檔案之間。似乎它查詢的最大時間長度有限,而您獲得的檔案數量取決於檔案被列舉的速度。
Frank Glck
1 年前
有時需要設定

ftp_set_option($this->connection_id, FTP_USEPASVADDRESS, false)

在您設定之前
ftp_pasv($this->connection_id, $state)
To Top