Using Phing To Deploy To FTP

31 January, 2010 | Phing

Although most developers might not like it FTP is quite a common way of deploying the files for a site to the server. To simplify this process Phing comes with a handy FTP transfer action called ftpdeploy.

In order to use the ftpdeploy we first need to install the Net_FTP package. To install this package just enter the following command:

pear install Net_FTP

The first thing we need to create is a build.properties file to store our ftp details

ftp.destination.host=ftp.theftphost.com ftp.destination.port=21 ftp.destination.username=theftpusername ftp.destination.password=theftppassword ftp.destination.dir=public_html ftp.destination.mode=binary

Below is a trivial example of ftpdeploy in action. The build file simply defines a single fileset, which contains a single file in a directory. The ftpdeploy task accepts the parameters of the build.properties file and uses the fileset to determine which files should be uploaded. The only option that might not be obvious is the mode option. This is for telling ftpdeploy which FTP transfer mode to use, these are either ascii or binary, the default value being binary.

<?xml version="1.0"?> <project name="FtpUpload" default="main">     <!-- Include properties file. -->     <property file="build.properties" />       <fileset dir="upload" id="TheFiles">         <include name="phpinfo.php" />     </fileset>       <ftpdeploy          host="${ftp.destination.host}"         port="${ftp.destination.port}"         username="${ftp.destination.username}"         password="${ftp.destination.password}"         dir="${ftp.destination.dir}"         mode="${ftp.destination.mode}">         <fileset refid="TheFiles" />     </ftpdeploy>       <target name="main">         <echo>FTP Upload Finished!</echo>     </target>  </project>

Assuming you have called your build file ftpupload.xml, you can run this Phing build file like this.

phing -f ftpupload.xml

This will produce the following output.

Buildfile: \ftpdeploy.xml  [property] Loading \build.properties   FtpUpload > main:        [echo] FTP Upload Finished!   BUILD FINISHED   Total time: 2.4250 seconds

There is an additional option called clearfirst which can be used to delete all of the files from the remote directory. The default value for this setting is "false". The Phing documentation states that once the directory has been cleared the set files will be uploaded. I have found that although Phing will clear the directory, it will not upload the files.

Have a look at the Phing documentation for more information about the Phing ftpdeploy task.

Written by Philip Norton.

Related posts:

2 Responses to “Using Phing To Deploy To FTP”

  1. Jens |
    I've been looking for something like this for a while - thanks for posting this.

    I have two questions:
    1. Do you know how long it would take to ftp a site that's about 100mb in size (I noticed you only transfer 1 file, hence the 2.5 seconds benchmark)
    2. Does setting the transfer mode to binary mean that text files are set to binary? and what about the permissions once they've been transfered?

    Thanks,
    Jens
  2. Philip Norton |
    In answer to your questions:
    1) Transferring 100mb would take as long as your connection allows.
    2) I would recommend you use binary as the default for FTP as the other setting (ascii) can do odd things to unicode text files.
    The following page quite a bit of detail on the subject:
    http://courses.wccnet.edu/computer/mod/na36c.htm

    Phing doesn't yet provide a mechanism to alter the permissions of a file. This must therefore be done with ftp_chmod(), which is part of the FTP functions available in PHP.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>