2012-10-31 56 views
0

我通过php的ftp连接连接到另一台服务器。通过php ftp远程递归搜索目录

不过,我需要能够提取所有HTML文件从它的Web根目录,这是造成我有点头疼......

我发现这个职位Recursive File Search (PHP)其中谈到使用RecursiveDirectoryIterator功能然而,这是与自己的php脚本位于同一服务器上的目录。

我已经受够了写我自己的功能,但不知道我有去是正确的......假设发送到方法的原始路径是服务器的文档根:

public function ftp_dir_loop($path){ 

    $ftpContents = ftp_nlist($this->ftp_connection, $path); 

    //loop through the ftpContents 
    for($i=0 ; $i < count($ftpContents) ; ++$i) 
     { 
      $path_parts = pathinfo($ftpContents[$i]); 

      if(in_array($path_parts['extension'], $this->accepted_file_types){ 

       //call the cms finder on this file 
       $this->html_file_paths[] = $path.'/'.$ftpContents[$i]; 

      } elseif(empty($path_parts['extension'])) { 

       //run the directory method 
       $this->ftp_dir_loop($path.'/'.$ftpContents[$i]); 
      } 
     } 
    } 
} 

有没有人看过预制课程来做类似的事情?

+0

这应该这样做,虽然NLIST()返回false像路径错误,无法找到或者是一个文件,你应该检查这一点。 –

+0

顺便说一句,也许更可靠的方法来检测目录是通过使用“-al $路径”作为第二个参数到ftp_nlist()。 –

回答

1

您可以尝试

public function ftp_dir_loop($path) { 
    $ftpContents = ftp_nlist($this->ftp_connection, $path); 
    foreach ($ftpContents as $file) { 
     if (strpos($file, '.') === false) { 
      $this->ftp_dir_loop($this->ftp_connection, $file); 
     } 
     if (in_array(pathinfo($file, PATHINFO_EXTENSION), $this->accepted_file_types)) { 
      $this->html_file_paths[$path][] = substr($file, strlen($path) + 1); 
     } 
    } 
} 
+0

我没有看到将文件本身的路径抛出的有用性。 –

+0

@Jack固定..... – Baba

+0

谢谢!非常感谢:) – John