简体   繁体   English

PHP ftp_get递归

[英]PHP ftp_get recursive

I'm having some difficulty getting recursion to work in this function: 我在使用此函数递归时遇到一些困难:

function fetchFiles($src_dir, $dst_dir) {
  global $conn_id;

  $src_dir = rtrim($src_dir, '/');
  $dst_dir = rtrim($dst_dir, '/');

  echo "fetching $src_dir into $dst_dir<br>";

  if(!is_dir($dst_dir)){
    mkdir($dst_dir);
    echo "create dir <b><u> $dst_dir </u></b><br>";
  } else echo "dir exists<br>";

  ftp_chdir($conn_id, $src_dir);
  $contents = ftp_nlist($conn_id, '.');
  foreach($contents as $file) {
    if ($file != "." && $file != "..") {
      if (is_dir($file)) {
        echo "fetch directory ".$src_dir."/".$file."<br>";
        fetchFiles($src_dir."/".$file, $dst_dir."/".$file);
      } else {
        ftp_get($conn_id, $dst_dir."/".$file, $src_dir."/".$file, FTP_BINARY);
        echo "create files::: <b><u>".$dst_dir."/".$file ." </u></b><br>";
      }
    }
    ob_flush();
    flush();
  }
}

I'm calling it like this: 我这样称呼它:

$uploads = "/web/content/wp-content/uploads/";
$local_base = "../wp-content/uploads/";
fetchFiles($uploads, $local_base);

The uploads directory has a lot of files, which transfer without any problem. uploads目录中有很多文件,这些文件的传输没有任何问题。 There is a folder named '2011', which gets created, as well. 还有一个名为“ 2011”的文件夹也将被创建。 The folders within '2011', however, are detected as files. 但是,“ 2011”中的文件夹被检测为文件。 The output says: 输出显示:

create files::: /web/content/wp-content/uploads/2011/02
create files::: /web/content/wp-content/uploads/2011/10
create files::: /web/content/wp-content/uploads/2011/12

'02', '10', and '12' are all subdirectories, with files inside them. '02','10'和'12'都是子目录,其中包含文件。 Why is it not detecting them as directories in the is_dir() check? 为什么在is_dir()检查中没有将它们检测为目录? What am I missing? 我想念什么?

Following Marc's advice, I was able to resolve this by using the following: 遵循Marc的建议,我可以使用以下方法解决此问题:

if (@ftp_chdir($conn_id, $file)) { ... }

Thank you Marc! 谢谢马克!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM