Skip to content

Blog

Using FTP across multiple locations

Author: Sirbastian Manning

Recently we built 2 separate websites that used the same CMS (content management system). The problem we had with this is when files were uploaded using the CMS, take images for example, they would upload to the website under which the CMS resided. Therefore the images could not be accessed by the second website.

The solution to this problem was to work out which site the images were being uploaded to and if it were the 2nd site, FTP them across. PHP allows you to easily do this. Firstly we need to open a FTP connection.

$aFtpCon = ftp_connect('ftp.example.com');

Next we need to login to this connection using the FTP username and password.

ftp_login($aFtpCon, 'myusername', 'mypassword');

So now we have a FTP connection and should have successfully logged in, if the username and password were correct. Now we can upload or put our file onto the server.

ftp_put($aFtpCon, 'path/to/file/' . $TheFile, $TheFileName, FTP_BINARY);

Our file should now be on the new server. There is one last thing we should do for good practise and that is to close the FTP connection.

ftp_close($aFtpCon);