2024 年 PHP Conference Japan

ftp_nlist

(PHP 4, PHP 5, PHP 7, PHP 8)

ftp_nlist傳回指定目錄中的檔案清單

說明

ftp_nlist(FTP\Connection $ftp, 字串 $directory): 陣列|false

參數

ftp

一個 FTP\Connection 實例。

directory

要列出檔案的目錄。這個參數也可以包含引數,例如:ftp_nlist($ftp, "-la /your/dir");。請注意,這個參數不會被跳脫字元,因此檔名中包含空格和其他字元時可能會出現問題。

回傳值

成功時返回指定目錄中檔案名的陣列,出錯時返回 false

更新日誌

版本 說明
8.1.0 ftp 參數現在需要一個 FTP\Connection 實例;以前需要一個 資源

範例

範例 #1 ftp_nlist() 範例

<?php

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

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

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

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

?>

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

array(3) {
  [0]=>
  string(11) "public_html"
  [1]=>
  string(10) "public_ftp"
  [2]=>
  string(3) "www"

參見

新增註釋

使用者提供的註釋 37 則註釋

匿名
14 年前
有些人抱怨 ftp_nlist 總是返回 FALSE。我自己也經歷過這種情況,直到我使用了 ftp_pasv,如果您的客戶端位於防火牆後方(現在大多數客戶端都是如此),這會很有用,然後 ftp_nlist 就能正常運作。我不太清楚使用 ftp_pasv 的所有含義,但如果您讀到或經歷過 ftp_nlist、ftp_get、ftp_nb_get 無法運作的情況,請嘗試新增以下內容

ftp_pasv($conn_id,true);
tobrien at florahill dot vic dot edu dot au
21 年前
ftp_nlist() 或 ftp_rawlist() 需要很長時間才能執行,然後卻沒有返回任何內容...

如果您遇到這個問題,您可能需要使用 ftp_pasv() 函式啟用 PASV 模式 FTP 傳輸。

範例...

<?php
$ftp_host
= "yourFTPHost";
$ftp_user = "yourUsername";
$ftp_password = "yourPassword";

//連線
echo "<br />透過 FTP 連線到 $ftp_host...";
$conn = ftp_connect($ftp_host);
$login = ftp_login($conn, $ftp_user, $ftp_password);

//
//啟用被動模式 (注意:必須在 ftp_login() 之後執行)
//
$mode = ftp_pasv($conn, TRUE);

//登入成功?
if ((!$conn) || (!$login) || (!$mode)) {
die(
"FTP 連線失敗!");
}
echo
"<br />登入成功。<br />";

//
//現在執行 ftp_nlist()
//
$file_list = ftp_nlist($conn, "");
foreach (
$file_list as $file)
{
echo
"<br>$file";
}

//關閉連線
ftp_close($conn);

?>
gerben at gerbs dot net
6 年前
請注意,自 PHP 7.2 起,有一個新的函式可用,它會將檔案列表以陣列形式返回,其中包含所有可直接使用的詳細資訊:https://php.dev.org.tw/manual/en/function.ftp-mlsd.php
zgardner at allofe dot com
14 年前
在執行 WAMP(Vista、Apache 2.21、MySQL 5.1.36、PHP 5.3)並從 LAMP FTP 伺服器(Ubuntu 8.04、Apache 2.2.8、MySQL 5.1.51a、PHP 5.2.4、VSFPTD)遞迴下載檔案時,我發現我的腳本在一段時間後停止下載檔案。我追蹤到是呼叫 ftp_nlist 導致腳本停止。

為了要解決這個問題,我使用了 ftp_pasv($ftp, true)。在用戶端或伺服器的 VSFTPD、Apache 或 PHP 錯誤日誌中都沒有任何紀錄。非常奇怪。
Jacob Slomp
12 年前
嗨,

我寫了這個函式來取得檔案列表,我只用 php5 和 linux ftp 測試過。對我來說運作良好。

它會返回一個陣列,包含名稱、類型(檔案/資料夾)、擁有者、擁有者 ID 和權限。

<?php
函數 ftp_get_filelist($con, $path){
$files = 陣列();
$contents = ftp_rawlist($con, $path);
$a = 0;

如果(計數(
$contents)){
foreach($contents 作為 $line){

preg_match("#([drwx\-]+)([\s]+)([0-9]+)([\s]+)([0-9]+)([\s]+)([a-zA-Z0-9\.]+)([\s]+)([0-9]+)([\s]+)([a-zA-Z]+)([\s ]+)([0-9]+)([\s]+)([0-9]+):([0-9]+)([\s]+)([a-zA-Z0-9\.\-\_ ]+)#si", $line, $out);

如果(
$out[3] != 1 && ($out[18] == "." || $out[18] == "..")){
// 什麼都不做
} 其他 {
$a++;
$files[$a]['權限'] = $out[1];
$files[$a]['類型'] = $out[3] == 1 ? "檔案":"資料夾";
$files[$a]['擁有者ID'] = $out[5];
$files[$a]['擁有者'] = $out[7];
$files[$a]['修改日期'] = $out[11]." ".$out[13] . " ".$out[13].":".$out[16]."";
$files[$a]['名稱'] = $out[18];
}
}
}
返回
$files;
}
?>

您好,

Jacob
turigeza on yahoo com
19 年前
使用 ftp_nlist() 遞迴刪除目錄...
<?php
function ftp_rmdirr($path, $handle)
{
if (!(@
ftp_rmdir($handle, $path) || @ftp_delete($handle, $path)))
{
$list = ftp_nlist($handle, $path);
if (!empty(
$list))
foreach(
$list as $value)
ftp_rmdirr($value, $handle);
}
@
ftp_rmdir($handle, $path);
}
?>
如果這個功能能內建在 PHP 中就太好了。畢竟大多數時候我們都需要移除非空的目錄。我敢打賭,每個處理檔案系統的人都遇過這個問題。
aRc
21 年前
在 Windows 中,如果目錄名稱包含空格,ftp_nlist 會失效。要解決這個問題,請使用 ftp_chdir

//變更目錄
ftp_chdir($conn, "directory with spaces");
//然後直接列出
$contents = ftp_nlist($conn, "");
fsa at dwarf dot dk
8 年前
所以我寫了一個使用 ftp_nlist() 的簡單函式,來遞迴地列出指定目錄中的檔案。

/**
* 返回指定目錄中的檔案列表,不包含目錄。
*
* @param resource $ftp_stream
* FTP 連線的連結識別碼。
* @param string $directory
* 要列出的目錄。
* 請注意,此參數未經過跳脫處理,因此檔名中包含空格和其他字元時可能會出現一些問題。
* @param bool $recursive
* (可選) 如果設為 TRUE,則發出的命令將以遞迴方式執行。
*
* @return array
* 返回指定目錄中的檔名陣列。
*/
function ftp_files_list($ftp_stream, $directory, $recursive = false) {
$files = array();

$list = ftp_nlist($ftp_stream, $directory);
if (is_array($list)) {
// 移除點目錄。
$list = array_slice($list, 2);

foreach ($list as $filename) {
$path = $directory . '/' . $filename;
// 如果大小等於 -1,則它是一個目錄。
if (ftp_size($ftp_stream, $path) === -1) {
if ($recursive) {
$files = array_merge($files, ftp_files_list($ftp_stream, $path, $recursive));
}
} else {
// 移除根目錄路徑以確保所有檔案路徑都是相對的。
$files[] = substr($path, strpos($path, '/') + 1);
}
}
}

return $files;
}
bintjes at gmail dot com
17 年前
我製作了一個遞迴函式,使用 ftp_rawlist() 列出資料夾和子資料夾中的所有檔案。我使用了 ari 的以下函式 parse_rawlist()

function list_all_files($conn_id, $path){
$buff = ftp_rawlist($conn_id, $path);
$res = parse_rawlist( $buff) ;
static $flist = array();
if(count($res)>0){
foreach($res as $result){
// 驗證是否為目錄,如果不是,則添加到檔案列表中
if($result['size']== 0){
// 如果此檔案是資料夾,則遞迴呼叫函式
list_all_files($conn_id, $path.'/'.$result['name']);
}
} else {
// 這是一個檔案,添加到最終列表中
$flist[] = $result;
}
}
}
} } return $flist;
}
franck569 at free dot fr
18 年前
turigeza on yahoo com 的 ftp_rmdirr() 函式的更好版本。

這個函式會回傳 true 或 false,表示路徑是否已被移除。

function ftp_rmdirr($handle, $path)
{
if(!@ftp_delete($handle, $path))
{
$list = @ftp_nlist($handle, $path);
if(!empty($list))
foreach($list as $value)
ftp_rmdirr($handle, $value);
}

if(@ftp_rmdir($handle, $path))
return true;
else
return false;
}
vijay dot mahrra at fronter dot com
13 年前
我們需要取得所有子目錄中所有檔案的列表,但找不到方法可以做到這一點,所以我們自己寫了一個。

<?php
/**
* ftpRecursiveFileListing
*
* 取得給定 FTP 連線控制代碼和路徑下所有子資料夾中所有檔案的遞迴列表
*
* @param resource $ftpConnection FTP 連線控制代碼
* @param string $path 資料夾/目錄路徑
* @return array $allFiles 檔案列表,格式為:目錄 => $filename
* @author Niklas Berglund
* @author Vijay Mahrra
*/
function ftpRecursiveFileListing($ftpConnection, $path) {
static
$allFiles = array();
$contents = ftp_nlist($ftpConnection, $path);

foreach(
$contents as $currentFile) {
// 如果名稱中沒有點,則假設它是資料夾
if (strpos($currentFile, '.') === false) {
ftpRecursiveFileListing($ftpConnection, $currentFile);
}
$allFiles[$path][] = substr($currentFile, strlen($path) + 1);
}
return
$allFiles;
}
?>
juhakala at gmail dot com
16 年前
我注意到,如果工作路徑不同,這個函式將無法運作,因為 $dir 保留了整個路徑字串的值。這就是為什麼要加上 basename() 函式來去除路徑,這樣這個函式才能在子資料夾中運作,而不僅限於根資料夾。

function ftp_is_dir($dir) {
global $ftp_connect;
if (ftp_chdir($ftp_connect, basename($dir))) {
ftp_chdir($ftp_connect, '..');
return true;
} else {
return false;
}
}
nekrostar at gmx dot net
18 年前
這個函式會回傳一個陣列,包含目錄路徑以及該路徑下的所有檔案和子目錄。

function ftp_searchdir($conn_id, $dir)
{
if( !@ftp_is_dir( $conn_id, $dir ) ) {
die( 'FTP 伺服器上沒有此目錄' );
}
if( strrchr( $dir, '/' ) != '/' ) {
$dir = $dir.'/';
}
$dirlist[0] = $dir;
$list = ftp_nlist( $conn_id, $dir );
foreach( $list as $path ) {
$path = './'.$path;
if( $path != $dir.'.' && $path != $dir.'..') {
if( ftp_is_dir( $conn_id, $path ) ) {
$temp = ftp_searchdir( $conn_id, ($path), 1 );
$dirlist = array_merge( $dirlist, $temp );
} else {
$dirlist[] = $path;
}
}

}
ftp_chdir( $conn_id, '/../' );

return $dirlist;

}
----------------------------------------------------------------------
// 檢查目錄 ($dir) 是否存在
// 回傳 true 或 false

function ftp_is_dir( $conn_id, $dir )
{
if( @ftp_chdir( $conn_id, $dir ) ) {
ftp_chdir( $conn_id, '/../' );
return true;
} else {
return false;
}
}
dreamsavior at gmail dot com
19 年前
以下是一個使用 ftp_nlist 檢查給定路徑是否為目錄的函式:

<?php
function is_ftp_dir($itempath, $ftp_server, $ftp_user_name='anonymous', $ftp_user_pass='') {
if (empty(
$ftp_user_name)) {
$ftp_user_name = "anonymous";
}
if (
$itempath[0]!=="/") {
$itempath = "/".$itempath;
}

$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

$contents = ftp_nlist($conn_id, "$itempath");

if (!empty(
$contents[0])) {
$check = str_replace($itempath, "", $contents[0]);
if (!empty(
$check)) {
return
true;
}
}
ftp_close($conn_id);
}

// 範例:
if (is_ftp_dir("/pub/ftp/unknownitem", "ftp.nowhere", 'username', 'verysecret'')) {
// 執行與目錄處理相關的操作
} else {
// 目標項目是一個檔案…
}

?>
mail at martinauer dot net
18 年前
注意事項
陣列將包含完整路徑,而不僅僅是檔名。至少在我嘗試使用 PHP 4.3.11 時是這樣

ftp_nlist("www.example.com/docs/some/thing")

它會輸出類似以下的內容

[0]=>
string(41) "www.example.com/docs/some/thing/file1.htm"
[1]=>
string(41) "www.example.com/docs/some/thing/file2.htm"
[2]=>
string(41) "www.example.com/docs/some/thing/file3.htm"
[3]=>
string(41) "www.example.com/docs/some/thing/file4.htm"
[4]=>
string(41) "www.example.com/docs/some/thing/file5.htm"
rmaslo at archa dot cz
2 年前
某些 FTP 伺服器會將帶有重音符號的字元轉換為 "?" 字元。您可以嘗試…

<?php
$ftp
= ftp_connect($ftp_server);
$login_result = ftp_login($ftp, $ftp_user_name, $ftp_user_pass);
ftp_raw($ftp, 'OPTS UTF8 ON'); //設定伺服器以 UTF8 回傳結果
$contents = ftp_nlist($ftp, ".");
var_dump($contents);
?>
artiom1st at yahoo dot com
18 年前
有時,由於伺服器上的 NAT 或防火牆問題,您將無法取得資料夾內容。因此,您需要將 ftp 連線設定為被動模式。

<?php

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

// 使用用戶名和密碼登入
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// 啟用被動模式
ftp_pasv( $conn_id, true );

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

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

?>
wim dot dupre at skynet dot be
17 年前
在 MAC OS 1.4 (Tiger) 上的 php 5.2.2 似乎預設為主動 ftp。為了讓 ftp_nlist 正常運作,我需要強制 ftp 使用被動模式。
在 Windows XP 上使用 PHP 5.2.3 的預設行為似乎是被動 ftp。

致意,

wim
ralph at ralphje dot nl
17 年前
如果您因為某些 open_basedir 限制而收到錯誤,請使用以下程式碼

<?php
putenv
('TMPDIR=/tmp/');
$filelist = ftp_nlist($ftp, "./accounts");
?>

(如果 /tmp/ 是 open_basedir 中允許的目錄)

我花了一段時間才發現這一點。
kip at friendchip dot com
17 年前
如果您只是想找 ftp 版本的 is_dir,您可以查看 ftp_chdir 是否回傳 true 或 false。
sksafarath at impelsys dot com
18 年前
如果我們將第二個參數指定為完整目錄路徑,則它將回傳完整路徑的檔案列表,如果我們只想列出檔案,您可以執行以下程式碼
// 設定基本連線
$conn_id = ftp_connect($ftp_server);

// 使用用戶名和密碼登入
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
//如果您要顯示 /home/test/ 中的檔案
//使用以下程式碼將目錄指向 /home/test
ftp_chdir($conn_id, "test");
//接著將第二個參數設為 null,即可取得檔案列表。
// 取得目前目錄的內容
$contents = ftp_nlist($conn_id, "");

備註:這個方法在 PHP5 中對我有效
AndyM
17 年前
crysis-online.com 的 tom 要小心 - 你建議的方法似乎與伺服器有關。我目前正在嘗試連線到一個 proftpd 伺服器,它回傳的所有檔案大小似乎都是 -1。
tom at crysis-online dot com
17 年前
這個函式用於檢查物件是檔案還是資料夾,比以下方法快得多。

<?php
function is_ftp_dir($file, $ftp_connection){
if(
ftp_size($ftp_connection, $file) == '-1'){
return
true; // 是目錄
}else{
return
false; // 是檔案
}
}
?>
nigel (at) amanwithapencil (dot) com
17 年前
補充 kip 的文章,這個方法有效

if (!ftp_chdir($conn, $folder))
{
ftp_mkdir($conn, $folder);
ftp_chdir($conn, $folder);
}
fx dot menard at pleasenospam dot free dot fr
18 年前
ftp_nlist 函式的衍生用法:判斷給定路徑是否存在,以及是目錄還是檔案(使用基本的 ftp 函式並不容易做到)。這段程式碼不會發出任何警告或錯誤。它利用 ls 的 -d 選項來提高效率,並使用 -F 選項在擷取的路徑是目錄時附加斜線。

<?php
// 傳回指定 FTP 路徑的資訊
// 0 表示路徑不存在
// 1 表示路徑是檔案
// 2 表示路徑是目錄
function ftp_pathtype( $conn, $path )
{
$res = ftp_nlist( $conn, "-dF " . $path );
if ( isset(
$res[0] ) and
strlen( $res[0] ) )
{
if (
$res[0][ strlen( $res[0] )-1 ] === "/" )
// 是目錄
return 2;
else
return
1;
} else
return
0;
}
?>
robtherat at gmx dot de
18 年前
nekrostar at gmx dot net 提供的函式對我來說無效,所以我嘗試用自己的方法來做。我會使用 ftp_size 函式來檢查找到的檔案是否為目錄。

<?php
function ftplistdir($conn_id, $dir){
$fold_no = array(".", "..", "cgi-data", "comp", "zuern", "counter");
$list = ftp_nlist( $conn_id, $dir );
foreach(
$list as $file){
if (
ftp_size($conn_id, $dir ."/".$file)== -1){
if (
in_array($file, $fold_no)) {
print
$file ." 略過排除的目錄。<br>";
} else {
$geslist[]= $dir ."/". $file;
$temp=ftplistdir($conn_id, $dir ."/". $file);
$geslist=array_merge($geslist, $temp);
}
}else{
$geslist[]= $dir ."/". $file;
}
}
return
$geslist;
}
?>
希望這對任何人都有用
Robert
ryan at wonko dot com
22 年前
給在 Windows 伺服器上使用 PHP 的開發人員的注意事項:從 PHP 4.1.2 開始,在 Windows 版本的 PHP 中,ftp_nlist() 功能已損壞。它不會返回任何內容,即使相同的程式碼在 UNIX 上可以正常運作。因此,如果您為了找出為什麼該函式沒有返回任何內容而苦惱,請停止浪費時間,您沒有做錯任何事。希望這會在未來的版本中得到修復,儘管顯然從 4.0.6 版開始就一直存在這個問題。

關於此錯誤的更多資訊,請參見 http://bugs.php.net/bug.php?id=16057
webmaster at torbox dot info
20 年前
要使用自訂的 LIST 旗標,例如
LIST -aF ../*/
(列出受保護的公開目錄)
您可以使用
<?php
$list
= ftp_nlist($ftp, "-aF ../*/");
?>
我花了一段時間才想出這個方法 :-)
micksam7 at neodecoder dot com
20 年前
更正 king_killa at juggalos4life dot com 的一點說明,它不是 ftp_res,而是 ftp_size。

順便一提,這裡有一個完整的函式,可以取得伺服器上的所有檔案,然後將它們寫入一個陣列。

<?php
$ftp1
= ftp_connect('ftp.nowhere1230404.foo') or die("無法連線到伺服器");
ftp_login($ftp1,'foo','bar');
ftp_pasv($ftp1, TRUE); //使用被動模式較佳

//取得檔案!
echo "正在收集 Neodecoder 上的檔案<br>";
//設定預設值,以避免 PHP 抱怨。
$dir = "";

function
filecollect($dir,$filelist) {
global
$ftp1; //取得我們的 ftp 連線
$files = ftp_nlist($ftp1,$dir); //取得目錄中的檔案
foreach ($files as $file) {
$isfile = ftp_size($ftp1, $file);
if(
$isfile == "-1") { //判斷是檔案還是目錄?
$filelist = filecollect($dir.'/'.$file,$filelist); //如果是資料夾,則對其執行 filecollect
}
else {
$filelist[(count($filelist)+1)] = $file; //如果不是,則將其作為檔案添加到檔案列表中
}
}
return
$filelist;
}

$filelist = filecollect($dir,$filelist);

echo
"<pre>";
print_r($filelist);
echo
"</pre>";
?>

如果您試圖將所有檔案從一台 FTP 伺服器傳輸到另一台伺服器,這會非常方便。 嗯,這也是我一開始編寫這個腳本的原因。
neodeus at allesamarsch dot com
21 年前
這是一個讀取完整 FTP 伺服器(從根目錄開始)的範例。
函式 files($dir)
{
/*
如果未設定 $dir 或 $dir 為空
{
$dir=ftp_pwd($this->conn_id);
}
*/
unset($list);
unset($files);
unset($folders);
unset($folder);
unset($file);
@ftp_chdir($this->conn_id, $dir);
$dir = ftp_pwd($this->conn_id);
$list=Array();
$list=ftp_nlist($this->conn_id, "$dir");
$files = array();
$folders = array();
for ($i = 0; $i != count($list); $i++)
{
$entry = str_replace("//", "", $list[$i]);
if (@ftp_chdir($this->conn_id, $entry))
{
$folders[] = $entry;
ftp_chdir($this->conn_id, $dir);
}
else
{
$files[] = $entry;
}
}
print "\t".$dir." 中的檔案:

";
foreach ($files as $file)
{
print $file."<br>";
}
print "\t".$dir." 中的資料夾:

";
foreach ($folders as $folder)
{
print "\t".$folder."<br>";
ftp_chdir($this->conn_id, $dir);
$this->files($folder);
}
}
louis at ewens dot com
21 年前
如果您使用萬用字元或其他搜尋方式來尋找特定檔案,但檔案不存在,Solaris FTP 伺服器會在回應陣列中傳回「search*.xml: 找不到檔案或目錄」作為單一元素。這使得檢查檔案是否存在變得困難。

一種可能的解決方法是自行解析原始目錄列表。或者,您可以逐字檢查錯誤。但不同的伺服器可能會傳回不同的值。一種更簡單的補償方法是使用 ftp_size 命令來驗證傳回的檔案。

if ( ($filelist = ftp_nlist( $this->conn_id, $filesearch )) === FALSE )
{
Error( "無法取得檔案列表" );
return FALSE;
}
// 檔案實際存在嗎? 捕捉無效的 FTP 伺服器列表回應
if ( !count($filelist) || ftp_size( $this->conn_id, $filelist[0] ) == -1 )
{
Error( "沒有有效的檔案" );
return FALSE;
}
// 檔案確實存在,擷取它或執行您想做的其他任何操作
nigel (at) amanwithapencil (dot) com
17 年前
實際上,這樣做效果更好(在 ftp_chdir() 失敗時抑制錯誤)!

if (!@ftp_chdir($ftp_conn, $userfolder))
{
ftp_mkdir($ftp_conn, $userfolder);
ftp_chdir($ftp_conn, $userfolder);
}
rfr at ajato dot com dot br
21 年前
有些 FTP 伺服器不接受 ftp_nlist 命令,它們會傳回一個布林變數。但我認為所有伺服器都接受 LIST 命令,也就是 ftp_rawlist 命令。因此,您必須將 rawlist 解析為 nlist。我找到了一段由「fredrik」發佈的程式碼,但如果 FTP 伺服器上的檔案名稱中包含空格,則會出現錯誤... 所以這裡有一個更好的函式。
function rawlist_2_nlist($list)
{
$newlist = array();
reset($list);
while (list(,$row) = each($list))
{
$buf="";
if ($row[0]=='d'||$row[0]=='-'){
$buf = substr($row,55);
$newlist[]=$buf;
}
}

return $newlist;
}
ds at over dot hu
19 年前
另一個使用靜態變數的 filecollect 腳本。

<?php

function filecollect($conn_id,$dir='.') {
static
$flist=array();
if (
$files = ftp_nlist($cid,$dir)){
foreach (
$files as $file) {
if (
ftp_size($cid, $file) == "-1"){
filecollect($cid,$file);
} else
$flist[] = $file;
}
}
return
$flist;
}

$conn_id = ftp_connect('ftp.nowhere.com') or die("無法連線到伺服器");
if (@
ftp_login($conn_id, 'anonymous', 'pass@nowhere.com')){
$filelist = filecollect($conn_id);
echo
"<pre>";
print_r($filelist);
echo
"</pre>";
}

?>
webmaster at weltvolk dot de
17 年前
為了在不支援 ftp_size() 的遠端伺服器上實現檔案/資料夾區分,最好使用下面提到的 ftp_is_dir() 函式。

<?php

//識別目錄

function ftp_is_dir($dir) {
global
$ftp_connect;
if (
ftp_chdir($ftp_connect, $dir)) {
ftp_chdir($ftp_connect, '..');
return
true;
} else {
return
false;
}
}
$ftp_nlist = ftp_nlist($ftp_connect, ".");

//依字母順序排序

sort($ftp_nlist);
foreach (
$ftp_nlist as $v) {

//1. ftp_is_dir() 為 true => 目錄
if (ftp_is_dir($v)) {

//輸出為 [ 目錄 ]
echo "[ " . $v . " ]<br />\n";
}
}
foreach (
$ftp_nlist as $v) {

//2. ftp_is_dir() 為 false => 檔案
if (!ftp_is_dir($v)) {

//輸出為檔案
echo "" . $v . "<br />\n";
}
}
?>
webmaster at weltvolk dot de
17 年前
如何以字母順序取得檔案列表,並將所有目錄置頂

<?php
$ftp_nlist
= ftp_nlist($ftp_connect, ".");

//依字母排序

sort($ftp_nlist);
foreach (
$ftp_nlist as $v) {

//1. 大小為 '-1' => 目錄
if (ftp_size($ftp_connect, $v) == -1) {

//輸出為 [ 目錄 ]
echo "[ " . $v . " ]<br />\n";
}
}
foreach (
$ftp_nlist as $v) {

//2. 大小不為 '-1' => 檔案
if (!(ftp_size($ftp_connect, $v) == -1)) {

//輸出為檔案
echo "" . $v . "<br />\n";
}
}
?>

結果如下所示

[ a_mydirectory ]
[ b_mydirectory ]
[ c_mydirectory ]
...
a_myfile.htm
b_myfile.css
c_myfile.php
...
markw at altexa dot com
24 年前
您可以使用 ftp_nlist 進行萬用字元檔案列表,如下所示

$list=ftp_nlist($conn_id, "$dir/*.txt");
To Top