Using Phing To Deploy To FTP

31st January 2010

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

  1. ftp.destination.host=ftp.theftphost.com
  2. ftp.destination.port=21
  3. ftp.destination.username=theftpusername
  4. ftp.destination.password=theftppassword
  5. ftp.destination.dir=public_html
  6. 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.

  1. <?xml version="1.0"?>
  2. <project name="FtpUpload" default="main">
  3.     <!-- Include properties file. -->
  4.     <property file="build.properties" />
  5.  
  6.     <fileset dir="upload" id="TheFiles">
  7.         <include name="phpinfo.php" />
  8.     </fileset>
  9.  
  10.     <ftpdeploy 
  11.         host="${ftp.destination.host}"
  12.         port="${ftp.destination.port}"
  13.         username="${ftp.destination.username}"
  14.         password="${ftp.destination.password}"
  15.         dir="${ftp.destination.dir}"
  16.         mode="${ftp.destination.mode}">
  17.         <fileset refid="TheFiles" />
  18.     </ftpdeploy>
  19.  
  20.     <target name="main">
  21.         <echo>FTP Upload Finished!</echo>
  22.     </target> 
  23. </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.

  1. Buildfile: \ftpdeploy.xml
  2.  [property] Loading \build.properties
  3.  
  4. FtpUpload > main:
  5.  
  6.      [echo] FTP Upload Finished!
  7.  
  8. BUILD FINISHED
  9.  
  10. 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.

Comments

Permalink
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

Jens (Thu, 02/25/2010 - 00:43)

Permalink
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.

philipnorton42 (Thu, 02/25/2010 - 09:11)

Add new comment

The content of this field is kept private and will not be shown publicly.