PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 1, 2022

[FIXED] How to copy file from FTP server to another FTP server (platform independently)

 August 01, 2022     ftp, ftplib, python     No comments   

Issue

I have made a script which copy file from local machine to FTP server. I referred this link to make script Upload folders from local system to FTP using Python script, but now I want to copy file from FTP to another remote FTP machine on different location using Python script. How to do this?

File copy can be done using rsync command, but I want to do this using Python script.

Code:

import ftplib
import os

server = 'host'
username = 'name'
password = 'pass'
ftp = ftplib.FTP(server, username, password)
Path = 'path'#source
val = "/des/"#destination

def copy(source,destination):
     print(source)
     print(destination)
     os.chdir(source)
     ftp.cwd(destination)
     if "C:\\" in Path or "c:\\" in Path:
        ftp_path = (source).split("\\")[-1]
     else:
        ftp_path = (source).split("/")[-1]
     directory = destination+ftp_path
     mkdir(directory)
     ftp.cwd(directory)
     read = os.listdir(source)
     for file in read:
         print(file) 
         if "C:\\" in Path or "c:\\" in Path:
            Type = source + "\\" + file
         else:
            Type = source + "/" + file
         print(Type)
         print()
         if os.path.isdir(Type):#If Type is Folder then it will create new 
folder
             copy(Type,directory+"/")
         elif os.path.isfile(Type):#If Type is file then it will create file
             print(Type) 
             current_dir = ftp.pwd() + "/"
             f = Type
             fh = open(f, 'rb')
             ftp.storbinary('STOR %s' % current_dir + file, fh)
             fh.close()

def mkdir(path):
    #print(path)
    ftp.mkd(path)
copy(Path,val)



ftp.close()

Solution

In general, you cannot transfer a file from one remote FTP server to another remote FTP server, if FTP protocol is the only way you can access the machines.

There's FXP protocol that allows that, but that's typically not allowed on most FTP servers.

If you have another access to one of the servers, like SSH, you can of course automatically login to the server and then run FTP client on it, to upload/download to/from the other server.

See also FTP copy a file to another place in same FTP.



Answered By - Martin Prikryl
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing