PHP使用ftp_u函数迭代目录
好了,伙计们,这是我遇到的另一个调试噩梦,我似乎无法准确地理解这里到底发生了什么-_- 基本上,这段代码将JSON返回到我的ajax请求中。它最初涉及来自php的一个数组PHP使用ftp_u函数迭代目录,php,json,conditional,ftp-client,Php,Json,Conditional,Ftp Client,好了,伙计们,这是我遇到的另一个调试噩梦,我似乎无法准确地理解这里到底发生了什么-_- 基本上,这段代码将JSON返回到我的ajax请求中。它最初涉及来自php的一个数组 array( "public_html"=> array( "public_html"=> array(//...other files actually in public html), ), 0 =>
array(
"public_html"=>
array(
"public_html"=>
array(//...other files actually in public html),
),
0 =>
array(
"filename":".ftpquota",
"filesize": 12kb
),
);
正如你所看到的,它创建了一个public_html索引,然后添加了另一个与public_html中的实际文件同名的索引。这不是我想要的,我希望原始public_html作为public_html中的文件返回
我希望这是部分容易理解的,我做了一堆注释用于调试目的。我需要迭代并在一个漂亮的JSON文件中正确地获取文件,这样我就可以用JS迭代。这是很难调试与互联网我有和测试它在IDE我使用,我只有一个chromebook。如果有人知道chromebook的好应用程序 快速破解:$files['public_html']=$files['public_html']['public_html'];其他人可能会发现原因并给出更好的解决方案。是的,我知道这一点,但真的找不到解决方案这是函数的第五次迭代更改周围的条件并添加它们当然快速破解不会起作用,因为我从技术上不知道文件名本身。我基本上只是使用ftp函数来更好地了解它们,如果我能从中获得API,我会的。
/*
Iterates over files and directories recursively
*/
function ftpFileList($ftpConnection, $path="/") {
//create general array to pass back later
$files = array();
//grabs the contents of $path
$contents = ftp_nlist($ftpConnection, $path);
//explode the $path to get correct index to fill in
$secondaryPath = explode('/',$path);
//test if the last index in the array is blank
//if it is pop it from the array
if($secondaryPath[count($secondaryPath) - 1] == "")
array_pop($secondaryPath);
//if the array is larger than or equal to one
if(count($secondaryPath) >= 1){
//we will rewrite $secondaryPath to the last index (actual file name)
$secondaryPath = $secondaryPath[count($secondaryPath) - 1];
}else{
//if it isn't anything we'll make it the path that we originally passed
$secondaryPath = $path;
}
//check for contents
if($contents){
//iterate over the contents
foreach($contents as $currentFile) {
//if the current file is not . or .. we don't need that at all
if($currentFile !== "." && $currentFile !== ".."){
//if there is a period in the currentFile this means it's a file not a directory
if( strpos($currentFile,".") == 0 ) {
if($files[""]){
if($secondaryPath == $currentFile)
$files[""][$secondaryPath][] = ftpFileList($ftpConnection, $path.$currentFile.'/');
else
$files[""][$secondaryPath][$currentFile] = ftpFileList($ftpConnection, $path.$currentFile.'/');
}else{
$files[$secondaryPath][] = ftpFileList($ftpConnection,$path.$currentFile.'/');
}
}else{
//here we know the $currentFile is a file
if($currentFile !== "." && $currentFile !== ".."){
//file in the correct index with a new index which is an array with information
$files[$secondaryPath][] = array(
"file"=>$currentFile,//file name
"filesize"=>humanFileSize(ftp_size($ftpConnection,"/".$path.$currentFile)),//human readable file size
"creation_date"=>date("F d Y g:i:sa",ftp_mdtm($ftpConnection,"/".$path.$currentFile)),//date in a human readable format
"full_path"=>"/".$path.$currentFile//full path of the file so we can access this later
);
}
}
}
}
return $files;//return the array
}
}