FTP from server to server using ColdFusion

Posted by: scoopseven 15 years, 10 months ago

Here's a brief set of instructions on how to ftp a file (or many files) from one server/computer to another using ColdFusion. On Windows: Download and install NcFtp Client from Microsoft Windows at: http://www.ncftp.com/download/ Make sure you're NcFtp directory is in your PATH statement so you can run it from anywhere. Test it by running something like this from a DOS prompt (where myHost is the IP address or host for your ftp server:
ncftpput -P -u myUser -p myPass myHost myDestinationFile.txt C:\myLocalFile.txt
Once you get NcFTP working, you want to create a .bat file and put your successful FTP command in it, let's call it autoFtp.bat. Now we're going to call autoFtp.bay from CF:
<cfset argumentsArray = arrayNew(1)> <cfexecute name = "c:\autoFtp.bat" arguments = "#argumentsArray#" timeout = "6000"> </cfexecute>
Don't worry about the argumentsArray, we'll get to that in a bit. Put the above code in a .cfm file and run it. Your file should successfully ftp to the remote server. When you can do that, you can start passing in arguments to your autoFtp.bat file. Edit autoFtp.bat to look like this:
ncftpput -P -u myUser -p myPass myHost %1 %2
Edit your .cfm page so it looks like this:
<cfset argumentsArray = arrayNew(1) <cfset argumentsArray[1] = "myDestinationFile.txt"> <cfset argumentsArray[2] = "C:\myLocalFile.txt"> <cfexecute name = "c:\autoFtp.bat" arguments = "#argumentsArray#" timeout = "6000"> </cfexecute>
When you pass argumentsArray to autoFtp.bat, you can reference them by their array index, so argumentsArray[1] will be referenced as %1 in autoFtp.bat. On Linux: A little bit easier, no need fro NcFTP. Instead, create a shell script, .sh file called autoFtp.sh. The contents are below:
#! /bin/sh DESTINATION_FILE=$1 LOCAL_FILE=$2 HOST='ftp.myhost.com' USER='myUserName' PASSWD='myPassword' ftp -nv <<EOF open $HOST user $USER $PASSWD binary put $LOCAL_FILE $DESTINATION_FILE EOF
Make sure you have execute permissions on the autoFtp.sh. I'm using this to transfer images, so the "binary" command is totally necessary for me.   The arguments are passed into autoFtp.sh on the command line, the first argument is referenced by $1, the second, $2, etc.  Good Luck!

Currently unrated


Recent Tweets

Recent Posts

Archive

2013
2012
2011
2010
2009
2008
2007
2006

Categories

Authors

Feeds

RSS / Atom