using coldfusion to manipulate files and directories james collins / alphainsight mdcfug dec 14,...
Post on 18-Dec-2015
213 views
TRANSCRIPT
Using ColdFusion to Manipulate Files and Directories
James Collins / AlphaInsight MDCFUG Dec 14, 2004
• Basic CF File Functions• Related File Functions• Using Java for File Handling• Applications and Examples• References
Presentation Overview
• Content Management System (FarCry - CMS) • Project Collaboration systems (eRoom)
- uploading Word documents and Excel spreadsheets
• Jobs site - uploading resumes• Scraping - storing remote content locally• Blogging software - append comments, upload
images• Working with XML
Why Use CF File Functions?
cffile action = "upload"
syntax :<cffile action = "upload" fileField = "formfield" destination = "full_path_name" nameConflict = "behavior" accept = "mime_type/file_type" mode = "permission" attributes = "file_attribute_or_list">
fileField = "formfield“
The name of the form element (i.e. form.filename)
specifing the file to upload and it's name
(this is a security precaution so that the types of files that can
be uploaded can be limited)
destination = "full_path_name"
Pathname of directory in which to upload the file. If not an
absolute path (starting a with a drive letter and a
colon, or a forward or backward slash), it is relative to the
ColdFusion temporary directory, which is returned
by the GetTempDirectory function.
nameConflict = "behavior"
Possible values: error, skip, overwrite, makeunique
cffile action = "upload"
accept = "mime_type/file_type“gives ablity to control what file types can be uploaded example: accept = "image/jpg, application/msword"
mode = "permission" Linux/Unix chmod values i.e. 666
attributes = "file_attribute_or_list">Windows only - values "readonly", "normal“
If has the same name as existing file - ColdFusion MX now makes filenames unique by appending a incrementing number, 1 for the first file, 2 for the second and so on, to the name. In older version of ColdFusion, filenames were made unique by appending an additional "1" for each file, as in 1, 11, 111, and so on."
CFFILE can be disabled in administrator security issue in shared hosting environment – reading/manipulating other peoples filesmalicious upload of .cfm file , then running itThe administrator can control cffile by using Sandbox Security.
cffile action = "upload"
Example:FORM PAGE<form method="post" action="tag_cffile_upload.cfm" name="uploadForm" enctype="multipart/form-data"><input name="FileContents" type="file"><br><input name="submit" type="submit" value="Upload File"> </form>
POST PAGE<cffile action = "upload" fileField = "FileContents" destination = "c:\files\upload\" accept = "text/html" nameConflict = "MakeUnique">
<input type="file"> automagically adds Browse button to form
cffile action = "upload"
<cffile action = "append" file = "full_path_name" output = “test string" addNewLine = "Yes" or "No" attributes = "file_attributes_list" mode = "mode" charset = "charset_option" >
addnewline = append newline character?
can include html, cfml, javascript
append - append to existing file
copy – copy file on server<cffile action = "copy" source = "full_path_name" destination = "full_path_name" mode = "mode" attributes = "file_attributes_list">
move - move file on server<cffile action = "move" source = "c:\files\upload\keymemo.doc" destination = "c:\files\memo\">
read - read file on server
Note - reads entire file into memoryThis is a possible problem if file is large - can crash serverCan cause time-consuming file io
<cffileaction = "read"file = "full_path_name"variable = "var_name"charset = "charset_option" >
readBinary – read binary file<cffileaction = "readBinary"file = "full_path_name"variable = "var_name">
Example:<cffile action = "readBinary" file = "C:\Inetpub\wwwroot\cfdocs\getting_started\photos\somewhere.jpg" variable = "aBinaryObj">
<!--- Output binary object to jpg format for viewing ---><cffile action="write" file = "c:\files\updates\somewhereB.jpg" output="#toBinary(aBinaryObj)#">
<!--- HTML to view image ---><img src="C:\files\updates\somewhereB.jpg">
rename - rename a file <cffileaction = "rename"source = "full_path_name"destination = "path_name"mode = "mode" attributes = "file_attributes_list">
If destination is a directory the file is only moved and not renamed
write - write to a file on server<cffileaction = "write"file = "c:\files\Lovecraft\cthulhu\cthulhu.txt"output = "In his house at R'lyeh dead Cthulhu waits dreaming"mode = "permission"addNewLine = "Yes" or "No"attributes = "file_attributes_list"charset = "charset_option" >
cfdirectory
<cfdirectoryaction = "directory action"directory = "directory name"name = "query name"filter = "list filter"mode = "permission"sort = "sort specification"newDirectory = "new directory name">
cfdirectoryActions:• list - returns a query record set of the files in the specified • directory. • create • delete • rename
Name: query name for listfilter: i.e. "*.cfm"sort: like "order by" SQL parameterexample: sort = "dirname ASC, file2 DESC, size, datelastmodified"
cfdirectory
Cfdirectory returns the following fields in the query:
• #mydirectory.name#
• #mydirectory.size#
• #mydirectory.type#
• #mydirectory.dateLastModified#
• #mydirectory.attributes#
• #mydirectory.mode#
cfcontent<cfcontent type = "file_type" deleteFile = "Yes" or "No" file = "filename" reset = "Yes" or "No">
Example:<cfcontent type = "text/html" file = "c:\inetpub\wwwroot\cfdocs\main.htm" deleteFile = "No">
Useful for returning type xls, doc, htmlBrowser will attempt to open using application
Mime Types: application/excelapplication/msword
Related FunctionsGetting directory information on the serverGetBaseTemplatePath()GetCurrentTemplatePath()Expandpath()GetDirectoryFromPath()
example:<cfset thisPath=ExpandPath("*.*")><cfset thisDirectory=GetDirectoryFromPath(thisPath)><cfoutput>The current full path is : #thispath# <br />The current directory is: #GetDirectoryFromPath(thisPath)# <br /></cfoutput>
Manipulating Windows registry - cfregistry
cfregistrycfregistry action = "getAll" cfregistry action = "get" cfregistry action = "set" cfregistry action = "delete"
CFMX XML Functions<cfxml><XmlParse><XmlNew><XmlTransform>
complete list:http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/xml.htm
cffile alternatives
What if my hosting provider doesn't allow cffile?How can I process large files?
Welcome to Java• Bypasses cffile restrictions• Processes files line by line
Using the java.io libraryouro.cfm
<CFSET thisPath= ExpandPath("*.*")> <CFSET thisDirectory= GetDirectoryFromPath(thisPath)> <cfset fileAsString = ""><cfset fileToRead = "#thisDirectory#ouro.cfm"><cfscript>fileReader = createObject("java", "java.io.FileReader");fileReader.init(fileToRead);bufferedReader = createObject("java", "java.io.BufferedReader");bufferedReader.init(fileReader);try { do { fileAsString = bufferedReader.readLine(); processLine(fileAsString); } while (true);} catch (coldfusion.runtime.UndefinedVariableException e) {// this indicates end of file, ok to ignore error }</cfscript>
<cffunction name="processLine"> <cfargument name="line" required="true"> <cfoutput>#arguments.line#<br /></cfoutput></cffunction>
Applications and ExamplesCFCDoc
http://www.spike.org.uk/projects/cfcdoc/
CFFM
Demo: http://www.webworksllc.com/cffm/
Download: http://cfopen.org/projects/cffm
References
Cffilehttp://livedocs.macromedia.com/coldfusion/6.1/htmldocs/tags-p27.htm
Multiple File Upload with cffilehttp://www.devarticles.com/c/a/Cold-Fusion/Multiple-File-Upload-with-CFFILE/
Java file functionshttp://www.johnwbartlett.com/cf_tipsntricks/index.cfm?TopicID=96
http://www.creative-restraint.co.uk/blog/index.cfm?mode=entry&entry=E3EE09EF-CBC6-17C4-075331DBF60DCED9
CFMX XML Functionshttp://livedocs.macromedia.com/coldfusion/6.1/htmldocs/xml.htm