Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using ColdFusion to Manipulate Files and Directories James Collins / AlphaInsight MDCFUG Dec 14, 2004

Similar presentations


Presentation on theme: "Using ColdFusion to Manipulate Files and Directories James Collins / AlphaInsight MDCFUG Dec 14, 2004"— Presentation transcript:

1 Using ColdFusion to Manipulate Files and Directories James Collins / AlphaInsight MDCFUG Dec 14, 2004 jimcollins@gmail.com

2 Basic CF File Functions Related File Functions Using Java for File Handling Applications and Examples References Presentation Overview

3 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?

4 cffile cfdirectory cfcontent Basic ColdFusion file functions

5 cffile Actions of the cffile tag: Upload Append Copy Delete Move Read readBinary Rename Write

6 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">

7 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"

8 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 files malicious upload of.cfm file, then running it The administrator can control cffile by using Sandbox Security. cffile action = "upload"

9 Example: FORM PAGE <form method="post" action="tag_cffile_upload.cfm" name="uploadForm" enctype="multipart/form-data"> POST PAGE <cffile action = "upload" fileField = "FileContents" destination = "c:\files\upload\" accept = "text/html" nameConflict = "MakeUnique"> automagically adds Browse button to form cffile action = "upload"

10 <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

11 copy – copy file on server <cffile action = "copy" source = "full_path_name" destination = "full_path_name" mode = "mode" attributes = "file_attributes_list">

12 delete - delete file on server <cffile action = "delete" file = "full_path_name">

13 move - move file on server <cffile action = "move" source = "c:\files\upload\keymemo.doc" destination = "c:\files\memo\">

14 read - read file on server Note - reads entire file into memory This is a possible problem if file is large - can crash server Can cause time-consuming file io <cffile action = "read" file = "full_path_name" variable = "var_name" charset = "charset_option" >

15 readBinary – read binary file <cffile action = "readBinary" file = "full_path_name" variable = "var_name"> Example: <cffile action = "readBinary" file = "C:\Inetpub\wwwroot\cfdocs\getting_started\photos\somewhere.jpg" variable = "aBinaryObj"> <cffile action="write" file = "c:\files\updates\somewhereB.jpg" output="#toBinary(aBinaryObj)#">

16 rename - rename a file <cffile action = "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

17 write - write to a file on server <cffile action = "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" >

18 cfdirectory <cfdirectory action = "directory action" directory = "directory name" name = "query name" filter = "list filter" mode = "permission" sort = "sort specification" newDirectory = "new directory name">

19 cfdirectory Actions: list - returns a query record set of the files in the specified directory. create delete rename Name: query name for list filter: i.e. "*.cfm" sort: like "order by" SQL parameter example: sort = "dirname ASC, file2 DESC, size, datelastmodified"

20 cfdirectory Cfdirectory returns the following fields in the query: #mydirectory.name# #mydirectory.size# #mydirectory.type# #mydirectory.dateLastModified# #mydirectory.attributes# #mydirectory.mode#

21 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, html Browser will attempt to open using application Mime Types: application/excel application/msword

22 Related Functions Getting directory information on the server GetBaseTemplatePath() GetCurrentTemplatePath() Expandpath() GetDirectoryFromPath() example: The current full path is : #thispath# The current directory is: #GetDirectoryFromPath(thisPath)#

23 Manipulating Windows registry - cfregistry cfregistry cfregistry action = "getAll" cfregistry action = "get" cfregistry action = "set" cfregistry action = "delete"

24 Manipulating.ini files getprofilestring() setprofilestring()

25 CFMX XML Functions complete list: http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/xml.htm

26 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

27 Ouroboros

28 Using the java.io library ouro.cfm 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 } #arguments.line#

29 Other Uses for the Java.io library Manipulating zip files Image handling (png, jpg) Encryption

30 Applications and Examples CFCDoc http://www.spike.org.uk/projects/cfcdoc/ CFFM Demo: http://www.webworksllc.com/cffm/ Download: http://cfopen.org/projects/cffm

31 References Cffile http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/tags-p27.htm Multiple File Upload with cffile http://www.devarticles.com/c/a/Cold-Fusion/Multiple-File-Upload-with-CFFILE/ Java file functions http://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 Functions http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/xml.htm


Download ppt "Using ColdFusion to Manipulate Files and Directories James Collins / AlphaInsight MDCFUG Dec 14, 2004"

Similar presentations


Ads by Google