【问题标题】:PHP copy() giving me a warningPHP copy() 给我一个警告
【发布时间】:2015-12-04 10:52:41
【问题描述】:

我正在尝试将文件从 FTP 远程服务器复制到另一台 FTP 远程服务器,但收到警告:

Warning: copy(): The first argument to copy() function cannot be a directory in

我已经仔细检查了我的文件夹名称和文件路径,一切都是正确的。我可以在嵌套的foreach() 之外成功使用copy(),但是一旦我添加嵌套循环并放入copy(),它就会窒息。

这是我的代码:

// set-up basic connection
$one_connection = ftp_connect("ftp.***.com");
$two_connection = ftp_connect("ftp.***.com");

// login with username and password
$one_login_result = ftp_login($one_connection, $one_user, $one_pass);
$two_login_result = ftp_login($two_connection, $two_user, $two_pass);

// get a list of directories on ftp server
$directories = ftp_nlist($one_connection, "ftp_images/rsr_number/");

// log start time
$start_time = date('Hi');

// loop through and pull all images from each directory
foreach($directories as $dir)
{
    // get list of images in directory
    $images = ftp_nlist($one_connection, "ftp_images/img_number/".$dir);

    foreach($images as $img)
    {
        copy('ftp://user:pass@ftp.****.com/ftp_images/img_number/'  .$dir . '/' . $img, 'ftp://user:pass@ftp.***.com/public_html/temp/' . $img);
    }
}

ftp_close($one_connection);
ftp_close($two_connection);

为什么我会收到此警告?

另外,这是连接两个远程 FTP 服务器的正确方法吗?

编辑

这是var_dump()$directories

array(27) {
 [0]=>
 string(1) "#"
 [1]=>
 string(1) "a"
 [2]=>
 string(1) "b"
 [3]=>
 string(1) "c"
 [4]=>
 string(1) "d"
 [5]=>
 string(1) "e"
 [6]=>
 string(1) "f"
 [7]=>
 string(1) "g"
 [8]=>
 string(1) "h"
 [9]=>
 string(1) "i"
 [10]=>
 string(1) "j"
 [11]=>
 string(1) "k"
 [12]=>
 string(1) "l"
 [13]=>
 string(1) "m"
 [14]=>
 string(1) "n"
 [15]=>
 string(3) "num"
 [16]=>
 string(1) "o"
 [17]=>
 string(1) "p"
 [18]=>
 string(1) "r"
 [19]=>
 string(1) "s"
 [20]=>
 string(1) "t"
 [21]=>
 string(1) "u"
 [22]=>
 string(1) "v"
 [23]=>
 string(1) "w"
 [24]=>
 string(1) "x"
 [25]=>
 string(1) "y"
 [26]=>
 string(1) "z"
}

【问题讨论】:

  • $dir 和 $img 有值吗?
  • 是的,$img 给出了图像名称,$dir 给出了目录名称。我已经验证它是正确的。

标签: php ftp php4


【解决方案1】:

这里试试这个:

使用的类是here

<?php

// use object oriented method
// Class found here: http://geneticcoder.blogspot.com/2015/04/class-for-doing-ftp-stuff.html
$ftp1 = new ftp($host_one, $one_user, $one_pass);
$ftp2 = new ftp($host_two, $two_user, $two_pass);

//change to the correct path of the temp directory
$ftp2->change_dir("public_html/temp/");

// get a list of directories on ftp server
$directories = $ftp1->list_all("ftp_images/rsr_number/");

// loop through and pull all images from each directory
foreach($directories as $dir){

    // FTP almost always sends these dot-dealies, filter them out
    if($dir == "." || $dir == "..") continue;

    // change the current directory
    $current_dir = "ftp_images/rsr_number/$dir";
    $ftp1->change_dir($current_dir);

    // get list of images in directory
    $images = $ftp1->list_all();

    foreach($images as $img){

        // FTP almost always sends these dot-dealies, filter them out
        if($img == "." || $img == "..") continue;

        // copy the file from a to b
        // create a temporary file on the server to store the file we're moving
        $tmpname = realpath(dirname(__FILE__))."/".microtime();
        // temporarily put the file on the current server
        $file = $ftp1->get("$current_dir/$img", $tmpname);
        // move file from current server to remote server
        $ftp2->put($tmpname);
        // remove the file from the current server
        unlink($tmpname);

    }
}

$ftp1->close();
$ftp2->close();

【讨论】:

  • 我会试一试并回复您。
  • 快速提问;您的代码是否允许我将文件从一台远程 FTP 服务器移动到另一台 FTP 服务器?我正在处理三台服务器,一台保存所有图像,另一台用于将图像移动到其中,另一台将运行代码以移动所有内容。
  • @Mike 这正是这段代码的作用。我更改的主要内容只是跳过当前目录和父目录的部分。 if($img == "." || $img == "..") continue;
  • @Mike - 我为你在中间部分添加了一些 cmets。
  • 非常酷,非常感谢!我会在接下来的几个小时内试一试。浏览您的课程,我真的很喜欢它并计划继续使用它......所以感谢您发布它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-25
  • 2020-07-03
相关资源
最近更新 更多