Skip navigation

Blog > FTP across two websites

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.

<code>$aFtpCon = ftp_connect('ftp.example.com');</code>

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

<code>ftp_login($aFtpCon, 'myusername', 'mypassword');</code>

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.

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

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.

<code>ftp_close($aFtpCon);</code>