It is easy to transfer/upload a file from one server to an other from php scripts. You have to use php ftp functions. If you have ftp access to other server(host, username, password), then you can follow following steps to upload a file from your loacl or from remote server to any other server.
1.First connect to the server where you want to send the file as:
$ftp=ftp_connect(“ftp.yourdomain.com”);
This will connect your php script to the other server.
2.Now open the file which you want to transfer or upload as:
$fp=fopen(‘myimage.jpg’, “r”);
You can open any file even any remote file using full file path.
3.Now login to ftp host using “username” and “password”.
ftp_login($ftp, “username”, “password”);
4.Now upload the file as:
ftp_fput($ftp, “public_html/myimage.jpg”, $fp, FTP_BINARY);
This function will put the file in public_html folder with same file name but you can use any name instead of “myimage.jpg”, the file will transfer with new name.
5.At end close ftp and opened file.
ftp_close($ftp);
fclose($fp);
Note: Here you need to specify the “public_html” folder name as most ftp give access to the main cPanel folder but if your ftp direct login in “public_html” then you don’t need to specify the “public_html” folder, just put file name. If you you are upolading file to any internal folder then you specify that folder name. Suppose you want upload the file to images folder then follow this:
ftp_fput($ftp, “public_html/images/myimage.jpg”, $fp, FTP_BINARY);
Full Code:
// Connect to ftp host
$ftp=ftp_connect(“ftp.yourdomain.com”);
// Open the file which is to be transfer
$fp=fopen(‘myimage.jpg’, “r”);
// Login to ftp host
ftp_login($ftp, “username”, “password”);
// Transfer the file in the “public_html” folder
ftp_fput($ftp, “public_html/myimage.jpg”, $fp, FTP_BINARY);
// Close ftp and opened file
ftp_close($ftp);
fclose($fp);
?>