1

I'm trying to copy files from an FTP remote server to another FTP remote server and I'm getting a warning:

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

I've double checked my folder names and file path and all is correct. I can successfully use copy() outside the nested foreach(), but as soon as I add the nested loop and put the copy() it chokes.

Here's my code:

// 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);

Why am I getting this warning?

Also, is this the proper way to connect two remote FTP servers?

EDIT

Here's a var_dump() for $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"
}
Mike
  • 1,760
  • 5
  • 21
  • 40

1 Answers1

2

Here try this:

The class used is 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();
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • I'll give it a try and get back to you. – Mike Sep 08 '15 at 17:29
  • Quick question; will your code allow me to move files from one remote FTP server to another FTP server? I'm dealing withe three servers, one that hold all the images, another that I need to move the images to, and another that will run the code to move everything. – Mike Sep 08 '15 at 17:36
  • @Mike that's exactly what this code does. The main thing that I changed was just the part that skips the current and parent directory. `if($img == "." || $img == "..") continue;` – I wrestled a bear once. Sep 08 '15 at 17:41
  • @Mike - I added some more comments to that middle part for you. – I wrestled a bear once. Sep 08 '15 at 17:45
  • Very cool, thank you so much! I will give it a try in the next couple of hours. Looking through your class, I really like it and plan on using it going forward...so thank you for posting it. – Mike Sep 08 '15 at 17:46
  • Awesome! Glad I could help. If it works for you remember to give me a upvote or something :) – I wrestled a bear once. Sep 08 '15 at 17:54
  • Just tried your suggestion and received : `Warning: ftp_fget(): Can't open ftp_images/rsr_number/#/511-10010-019-L-XL_1.jpg: No such file or directory in` My guess is becaue it cannot find the path to actual remote server? – Mike Sep 08 '15 at 19:52
  • It gives me that error for every image BTW. There are 1000 images and the very last line shows : `Warning: ftp_fget(): Can't open ftp_images/` – Mike Sep 08 '15 at 19:53
  • @Mike - You may have to adjust the paths.. I wrote that code blind (not having access to your filesystem - meaning I couldn't test it) ..Do you have a folder called "#" on the filesystem? You might want to figure out where that's coming from and get rid of it. – I wrestled a bear once. Sep 08 '15 at 20:03
  • Gotcha, as an example to my remote server, would it be : `ftp.server.com/ftp_images/rsr_number/`? The # directory is used by a server I have no control over...not sure why they use it. – Mike Sep 08 '15 at 20:07
  • @Mike - I think I confused you by using that FTP class.. The main thing I wanted you to take from this was the part that skips the current and parent directory. Whenever you call `ftp_nlist()` (or in this case `$ftp->list_all()`) the FTP server returns the current directory as "." and the parent directory as "..". If you try to loop through the lists you're going to get caught up on these two unless you skip them. So whether you use my method or yours doesn't matter much, underneath it's the same FTP code. All you really needed to do was add those two lines that skip the dots. – I wrestled a bear once. Sep 08 '15 at 20:17
  • Once you get that working then you should look into converting the code to use the FTP class. – I wrestled a bear once. Sep 08 '15 at 20:17
  • I added a var_dump() to see if I see anything else other than the folders, and nothing shows up. I don't see '.' or '..' – Mike Sep 08 '15 at 21:13