Monday, 13 August 2018

Using Phing To Deploy To FTP

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.

0 comments:

Post a Comment