fel*_*yot 3 php ftp web-applications file
我试图从FTP服务器列出文件.我希望将一个子目录和文件数组作为树来获取,如下所示:
folder1
file1.txt
file2.txt
folder2
folder2a
file1.txt
file2.txt
file.3txt
folder2b
file1.txt
Run Code Online (Sandbox Code Playgroud)
现在我的数组将是类似的
[folder1]=>array(file1.txt,file2.txt)
[folder2]=>array([folder2a]=>array(file1.txt,file2txt,file3.txt)
[folder2b]=>array(file1.txt))
Run Code Online (Sandbox Code Playgroud)
注意:上面的数组可能不是确切的语法,只是为了让我知道我在寻找什么.我试过ftp_nlist()但似乎只返回文件和文件夹,但不返回子文件夹中的文件.以下是我的代码如何显示的示例
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// get contents of the ftp directory
$contents = ftp_nlist($conn_id, ".");
// output $contents
var_dump($contents);
Run Code Online (Sandbox Code Playgroud)
以上只有文件夹列表而不是文件列表.任何人都知道如何解决这个问题?谢谢.
ftp_nlist()
不会递归地获取文件和目录,它只返回指定路径上的所有文件和文件夹.您可以编写一个函数来以递归方式获取结果.这是一个有人编写的递归函数示例,我在PHP ftp_nlist()文档中找到了它:
<?php
/**
* ftpRecursiveFileListing
*
* Get a recursive listing of all files in all subfolders given an ftp handle and path
*
* @param resource $ftpConnection the ftp connection handle
* @param string $path the folder/directory path
* @return array $allFiles the list of files in the format: directory => $filename
* @author Niklas Berglund
* @author Vijay Mahrra
*/
function ftpRecursiveFileListing($ftpConnection, $path) {
static $allFiles = array();
$contents = ftp_nlist($ftpConnection, $path);
foreach($contents as $currentFile) {
// assuming its a folder if there's no dot in the name
if (strpos($currentFile, '.') === false) {
ftpRecursiveFileListing($ftpConnection, $currentFile);
}
$allFiles[$path][] = substr($currentFile, strlen($path) + 1);
}
return $allFiles;
}
?>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8536 次 |
最近记录: |