FTP copy a file to another place in same FTP

前端 未结 9 2035
闹比i
闹比i 2020-11-28 11:54

I need to upload same file to 2 different place in same FTP. Is there a way to copy the file on the FTP to the other place instead of upload it again? Thanks.

相关标签:
9条回答
  • 2020-11-28 12:27

    The code below makes the FTP server to upload the file to itself (using loopback connection). It needs the FTP server to allow both passive and active connection mode.

    If you want to understand the ftp commands here is a list of them : List of ftp commands

    function copyFile($filePath, $newFilePath)
    {
        $ftp1 = ftp_connect('192.168.1.1');
        $ftp2 = ftp_connect('192.168.1.1');
        ftp_raw($ftp1, "USER ftpUsername");
        ftp_raw($ftp1, "PASS mypassword");
        ftp_raw($ftp2, "USER ftpUsername");
        ftp_raw($ftp2, "PASS mypassword");
    
        $res = ftp_raw($ftp2, "PASV");
        $addressAndPort = substr($res[0], strpos($res[0], '(') + 1);
        $addressAndPort = substr($addressAndPort, 0, strpos($addressAndPort, ')'));
    
        ftp_raw($ftp1, "CWD ." . dirname($newFilePath));
        ftp_raw($ftp2, "CWD ." . dirname($filePath));
        ftp_raw($ftp1, "PORT ".$addressAndPort);
    
        ftp_raw($ftp1, "STOR " . basename($newFilePath));
        ftp_raw($ftp2, "RETR " . basename($filePath));
    
        ftp_raw($ftp1, "QUIT");
        ftp_raw($ftp2, "QUIT");
    }
    
    0 讨论(0)
  • 2020-11-28 12:33

    I don't think there's a way to copy files without downloading and re-uploading, at least I found nothing like this in the List of FTP commands and no client I have seen so far supported something like this.

    0 讨论(0)
  • 2020-11-28 12:33

    I managed to do this by using WebDrive to mount the ftp as a local folder, then "download" the files using filezilla directly to the folder. It was a bit slower than download normally is, but you dont need to have the space on your hdd.

    0 讨论(0)
  • 2020-11-28 12:37

    I can copy files between remote folders in Linux based systems. In my particular case, I'm using very common file manager PCManFM:

    • Menu "Go" --> "Connect to server"
    • FTP Login info, etc
    • Open new tab in PCManFM
    • Connect to same server
    • Copy from tab to tab...

    It's a bit slow, so I guess that it could be downloading and uploading back the files, but it's done automatically and very user-friendly.

    0 讨论(0)
  • 2020-11-28 12:40

    There's no standard way to duplicate a remote file over the FTP protocol. Some FTP servers support proprietary or non-standard extensions for this though.


    Some FTP clients do support the remote file duplication. Either using the extensions or via a temporary local copy of the remote file.

    For example WinSCP FTP client does support the duplication using both drag&drop and menu/keyboard command:

    • It supports the SITE CPFR/CPTO FTP extension (supported for example by the ProFTPD mod_copy module)
    • It falls back to an automatic duplication via a local temporary copy, if the above extension is not available.

    (I'm the author of WinSCP)


    Another workaround is to open a second connection to the FTP server and make the server upload the file to itself by piping a passive mode data connection to an active mode data connection. This solution is shown in the answer by @SaadAchemlal. This is basically use of FXP protocol, but for one server. Though many FTP servers will reject this, as they wont allow data connection to/from an address different to the client's.


    Side note: people often confuse move with copy. In case you actually want to move, then that's a completely different question. Moving file on FTP is widely supported.

    0 讨论(0)
  • 2020-11-28 12:40

    Yes, the FTP protocol itself can support this in theory. The FTP RFC 959 discusses this in section 5.2 (see the paragraph starting with "When data is to be transferred between two servers, A and B..."). However, I don't know of any client that offers this sort of dual server control operation.

    Note that this method could transfer the file from the FTP server to itself using its own network, which won't be as fast as a local file copy but would almost certainly be faster than downloading and then reuploading the file.

    0 讨论(0)
提交回复
热议问题