ColdFusion Tips and Tutorials

ColdFusion Tips and Tutorials. Tips on ColdFusion, AJAX, CSS, JavaScript, HTML, Design, and more.

CFUnited Developer Conference 2010
Use this code TIPSCUST to get $100 off your registration @ CFUnited! We'll see you There!


ColdFusion Tips
Page 1 2 3
148 ColdFusion, Ajax, FuseBox, Tips, and Tut
147 Included Point of View
146 Javascript - OnFocus
145 Nathan's Rules of Professional Web Desig
144 Universal Server-Side Check | Bandwidth
143 Meeting Schedule | Identification Sessio
142 Breaking Frames Without Javascript
141 Unreal Forms
140 Screen Resolution
139 Human Help
138 Better Server-Side Validation
137 Automatic Server-Side Validation
136 Regular Expression Laboratory,ColdFusion
135 Rank-Ordered Site Search
134 Building Cryptograms
133 Well-Formed Includes
132 Grouping Families for Visits
131 Display Families on a Map Grid
130 Slide Shows
129 Determine Your Database Engine
128 ColdFusion in Context: Maxlength Lies
127 Something Extra
126 Parsing Database Structure from Data Def
125 Valid Values Maintenance
124 Print 1
123 Hide Session Id
122 Downsizing Data to Access
121 Time to Load a Page, FuseBox 4
120 Order and Rank by Subset
119 Warn through E-mail & Update on Paste Sp
118 Paste Spreadsheets, ColdFusion Component
117 Review Files Having Fixed-Length Fields
116 Organized Help
115 Sequence Slider
114 Bad Bits
113 Logical Deduction
112 Whiteout
111 Forced Navigation
110 Managing Permissions
109 Time Travel
108 Test First
107 Get Distance Between Map Coordinates
106 Validating Checkboxes
105 Matrix Manipulation
104 Field Help
103 Fake Object Not Found
102 Rank Order Correlation Coefficient
101 From Calling Pairs to Calling Tree
100 Posting Notice
99 Logout Persuasion
98 Release Session Memory
97 Use Identically Named Fields
96 Web Bug
95 Password Generation
94 Core Queries
93 Use CFFTP
92 Insert, Update, and Delete
91 Stack
90 T-Value
89 Bulk Data Entry and E-mail Validation
88 Quick Reset
87 Design 1
86 Use CFFTP
85 Support Login with AutoPost
84 Login and Site Protection
83 XY Graphs in a Graphing Calculator
82 Read Encrypted Files
81 Showing Progress
80 Frugal Cross-Browser Javascript
79 Tabbed Folders
Page 1 2 3



Custom Search
ColdFusion TIPS PLUS


Issue 00093 http://www.cftipsplus.com

I. My Comments

II. ColdFusion In Context: Use CFFTP
By R. Martin Ladner
martin.ladner@knology.net



See our sponsors at the bottom of this e-mail.
See offer special just for cftipsplus.com readers.


I. Comments:

Last week I was sick in bed all weekend. I missed two days of work and I am still getting over what ever it was I had. Hope your doing well.

Keep Coding,
Nathan Stanford
President/CEO
C.F. Concepts, Inc.
http://www.cftipsplus.com
http://www.htmlostips.com

If you have suggestions for articles send them to us.
If you would like to write for cftipsplus.com
send us an email to:

admin@cftipsplus.com

IF YOU WANT TO BE AN AUTHOR SEND IN YOUR COLDFUSION TIPS.

Remember this is a great way to get your name known in the
ColdFusion Community.



Advanced, Intensive ColdFusion Training!

Visit this site. If you have plans to get training here is a company that provides Advanced, Intensive ColdFusion Training. Check them out.

http://www.coldfusiontraining.com/index.cfm?ref=cftipsplus



CFM - The ColdFusion Monthly!
http://www.ColdFusionMonthly.com

By becoming a .CFM subscriber, you'll receive. ColdFusion information you will not find anywhere else:
Tips, tricks, techniques, strategies, and a wealth of knowledge you will be able to apply immediately to your work, all from established developers.

Monthly access to new articles written by industry-leading ColdFusion experts. A fully-functional application that accompanies each issue.

Full access to all issues, past and present (with a one year paid subscription). The chance to be seen in our "Community Spotlight" corner, which showcases new writers and emerging talent


If you have any suggestions please email me at cftips@nsnd.com.


II. ColdFusion in Context: Use CFFTP
By R. Martin Ladner
martin.ladner@knology.net



Suppose you wanted to pull and display files from a machine that doesn't have a Web server loaded. One way to do that is with the cfftp tag. This tip lets you view a local directory with cfdirectory, delete local files with cffile, view a remote directory with cfftp, copy files from that directory with cfftp, and display them javascript to pop open a new window.

Outline the Viewer
Here's an outline to follow to juggle all these tasks. Put the pop-up script in the document header so it will be ready when you need it. Invoke session management as one way to give the ftp connection a unique name that no other user will step all over while you're using it. When the user presses a submit button on the form, you'll need to test for that variable and act on it before the form reloads. To give the user feedback and controls, you'll need to query the remote and local directories. You'll need to use the queries to embed directory lists and file controls into the form. Finally, the display buttons will need to be set to invoke the javascript routine you placed in the header.

Pop-Up
Nest script tags within an HTML header and place the pop-up function within that. The word "page" is a placeholder for the value to be passed by form controls for display. Begin "view.cfm" with this code.

<head>
<script language="javascript">
function pop(page) {
popWindow = window.open(page, 'popWin');
}
</script>
</head>

Establish Defaults
Open the body and session, and set common defaults; add this code to view.cfm. The application name can be anything not repeated elsewhere on your server. Load login information from an included file. This way, if the Web server hiccups and tries to serve the unterpreted content of view.cfm, the login information won't be seen. To simplify installation, set the "scriptdir" to the full drive and directory path leading to this page.

<body>
<cfapplication name="ftp" sessionmanagement="yes">

<cfinclude template="howtoftp.cfm">
<cfset scriptdir=getDirectoryFromPath(#cgi.cf_template_path#)>

Open and Act
Set key form variables if undefined, open the session, and act; add this code to view.cfm. If the user asked to copy a file from the remote server, change remote directories and get the file. If the user asked to delete a file from the local server, delete the file.

<cfparam name="form.myget" default="">
<cfparam name="form.mydel" default="">

<cfinclude template="ropen.cfm">

<cfif len(form.myget)>
<cfinclude template="rcd.cfm">
<cfinclude template="rget.cfm">
<cfelseif len(form.mydel)>
<cfinclude template="ldel.cfm">
</cfif>

Build a Form and Close the Connection
Query remote directories, close the ftp connection, and build a form based on the queries. Add this code to view.cfm. The directory-seeking code will be in rls.cfm and ldir.cfm. The form has three different kinds of controls: a submit button (used for refresh) that doesn't set anything important, submit buttons that pass a filename for copy ("myget") or delete ("mydel"), and button buttons [grin] that display a file ("myshow") in a window through the use of the javacript routine invoked earlier.

To place the files in context, information about the files (such as their date and size) is shown. The information for both remote and local directories is accessed as one would access a database query. However, the names and data types for some of this information varies depending on the source. The date and time of a file in the remote directory is a real date object that has to be parsed for display; the corresponding element for the local directory is a string already, and it doesn't have the same name. The file size is called Length for the remote query and Size for the local query. Finally, to avoid seeing names such as . and .. in the local display, they're filtered out with a contains statement. Close the body of the document.


<cfinclude template="rls.cfm">

<cfinclude template="rclose.cfm">

<cfinclude template="ldir.cfm">

<form name="view" action="view.cfm" method="post">
<input name="lookagain" type="submit" value="Refresh">
<p>
REMOTE DIRECTORY<br>
Copy of these from the remote server.<br>
<cfoutput query="remotedir">
Copy - <input name="myget" type="submit" value="#Name#">,
#DateFormat(LastModified,"mm/dd/yyyy")#
#TimeFormat(LastModified,"hh:mm:ss tt")#,
#Length#<br>
</cfoutput>
<p>
LOCAL DIRECTORY<br>
Display or delete one of these from the local server.<br>
<cfoutput query="localdir">
<cfif ".." does not contain Name>
Display - <input name="myshow" type="button"
value="#Name#" onClick="javascript:pop('stuff/#Name#');">
,
#DateLastModified#, #Size# - <input name="mydel" type="submit"
value="#Name#">
- Delete<br>
</cfif>
</cfoutput>
</form>
</body>

Set Login Data
Populate the following variables with real data; call this code howtoftp.cfm

<cfset request.server="MyFtpServerNameOrIP">
<cfset request.username="MyUsername">
<cfset request.password="MyPassword">
<cfset request.workdir="RemoteDirRelativeToLogin">

Open the Connection
The cfftp tag lets you open and name a connection that you can then re-use with other ftp commands until the remote server times breaks it. Enabling session management as you did in the calling page made it possible to use the sessionID as the connection name. (You could use the createuuid function instead to get a unique name and then use a hidden form field to pass it alone if you planned to use the same connection for subsequent pages.) In coding the open statement, set a timeout, let it try a few times, and set stoponerror to no so it its errors won't halt the page. If it doesn't succeed, let cfftp tell you why with cfftp.errortext. Name this code ropen.cfm

<cfftp connection="#session.sessionID#" action="open" server="#request.server#"
username="#request.username#" password="#request.password#"
timeout="30" retrycount="3" stoponerror="no">

<cfif cfftp.errorcode is not 0>
The connection did not succeed;
<cfoutput>#cfftp.errortext#</cfoutput>
<cfabort>
<cfelse>
Connected OK.
</cfif>

Change Remote Directories
While the connection is still open, you can perform multiple ftp operations without logging in again. Thus, the login information doesn't have to be specified over again for changing a directory right away. Call this code rcd.cfm.

<cfftp connection="#session.sessionID#" action="changedir" directory="#request.workdir#"
timeout="30" retrycount="3" stoponerror="no">

<cfif cfftp.errorcode is not 0>
<cfoutput>Directory not changed to #request.workdir#:
#cfftp.errortext#
</cfoutput>
<cfabort>
<cfelse>
<cfoutput>Changed directory to #request.workdir#</cfoutput>
</cfif>

Get a File
For the get function, both the remote file and the name it will have on the local server must be specified. The transfer mode is set explicitly here for clarity, but cfftp will usually make the right choice based on file extension anyway (and its default list of extensions with which ASCII mode should be used is easily changed). Call this code rget.cfm.

<cfftp connection="#session.sessionID#" action="getfile"
remotefile="#form.myget#" localfile="#scriptdir#/stuff/#form.myget#" transfermode="binary"
timeout="30" retrycount="3" stoponerror="no" failifexists="no">

<cfif cfftp.errorcode is not 0>
<cfoutput>
Attempt to get #form.myget# failed.
#cfftp.errortext#
</cfoutput>
<cfelse>
Got <cfoutput>#form.myget#</cfoutput> OK.
</cfif>
<cfset form.myget="">

Delete a Local File
The command to delete a local file is actually a one-liner that's pulled out into a separate file for clarity. The file parameter consists of the path to the script (set by view.cfm), the directory below it that holds these files, and the name of the file. Call this code ldel.cfm.

<cffile action="delete"
file="#scriptdir#/stuff/#mydel#">

Query a Remote Directory
The action to query a remote directory produces a query that you can use in the same manner as a database query. Reference the name parameter used here as the name of the query. Call this code rls.cfm.

<cfftp connection="#session.sessionID#" action="listdir"
directory="#request.workdir#" name="remotedir" timeout="30" retrycount="3" stoponerror="no">

<cfif cfftp.errorcode is not 0>
Remote listing did not succeed.
<cfoutput>
#cfftp.errortext#
</cfoutput>
<cfelse>
Remote listing was OK.
</cfif>

Close a Remote Connection
If you don't close the connection, it will tie up resources of two computers until the remote server eventually times it out. Call this code rclose.cfm.

<cfftp connection="#session.sessionID#"
action="close">

Query a Local Directory
The action to query a remote directory produces a query that you can use in the same manner as a database query. Reference the name parameter used here as the name of the query. Call this code ldir.cfm.

<cfdirectory action="list" directory="#scriptdir#/stuff"
name="localdir">

Try Out the Demo
To make this work, you'll need directories on two servers and this code. Put some pictures in the remote directory and correct this code to match that directory name. Put this code on your local server, add a directory below it, and correct this code to match the local directory name. Then click on view.cfm. Get some files (copies) from the remote server, display them locally, and delete local copies.
Findings
The ftp protocol is tough to test when you don't own your own servers. In general, I had good results with list, get, and delete. All my put attempts resulted in zero-length files (which is why I didn't use "put" in the demo), but that was probably due to firewall and permission issues. The cfftp tag is otherwise easy and straightforward to use.
=Marty=



SPONSOR ADS:
This e-mail is sponsored by the following ads.

Sponsored Ad



IT'S A WEB DEVELOPER'S DREAM COME TRUE

The book on Aestiva's web-based development engine
is here. Build web-based text editiors in ten lines
or less. Build shopping carts with a couple dozen
lines of code. Build database-driven apps in about
the time it takes you to do your laundry! Get the
book ADVANCED WEB SITES MADE EASY. For more info
please visit:

http://dev.aestiva.com/amazon/htmlostips.html

Buy the book Advanced Web Sites Made Easy and
get $200 off your first purchase of HTML/OS.
Offer valid for CFTIPSPLUS.COM users only.


Sponsored Ad


Looking for other development resources?
Visit http://www.htmlostips.com, a site dedicated
to HTML/OS, a the next generation development
environment that many are saying is the next
generation to Java.

Sponsored Ad


Publisher and Creator:
Nathan Stanford,
admin@cftipsplus.com
http://www.cftipsplus.com | http://www.htmlostips.com

Macromedia and ColdFusion are U.S. registered trademarks.


Copyright (c) 2000 - 2001 CFTIPSPLUS.COM and HTMLOSTIPS.COM
Permission is granted to circulate this publication via
MANUAL forwarding by email to friends provided that the text is
forwarded in its entirety and no fee is charged.

Photo of Nathan Stanford
Nathan Stanford
LinkedIn

R. Marty Ladner's
Site