Using Cold Fusion to Manipulate Files and Directories

  • Slides: 31
Download presentation
Using Cold. Fusion to Manipulate Files and Directories James Collins / Alpha. Insight MDCFUG

Using Cold. Fusion to Manipulate Files and Directories James Collins / Alpha. Insight MDCFUG Dec 14, 2004 jimcollins@gmail. com

Presentation Overview • • • Basic CF File Functions Related File Functions Using Java

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

Why Use CF File Functions? • • • Content Management System (Far. Cry -

Why Use CF File Functions? • • • Content Management System (Far. Cry - CMS) Project Collaboration systems (e. Room) - uploading Word documents and Excel spreadsheets Jobs site - uploading resumes Scraping - storing remote content locally Blogging software - append comments, upload images Working with XML

Basic Cold. Fusion file functions • cffile • cfdirectory • cfcontent

Basic Cold. Fusion file functions • cffile • cfdirectory • cfcontent

cffile Actions of the cffile tag: • Upload • Append • Copy • Delete

cffile Actions of the cffile tag: • Upload • Append • Copy • Delete • Move • Read • read. Binary • Rename • Write

cffile action = "upload" syntax : <cffile action = "upload" file. Field = "formfield"

cffile action = "upload" syntax : <cffile action = "upload" file. Field = "formfield" destination = "full_path_name" name. Conflict = "behavior" accept = "mime_type/file_type" mode = "permission" attributes = "file_attribute_or_list">

cffile action = "upload" file. Field = "formfield“ The name of the form element

cffile action = "upload" file. Field = "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 Cold. Fusion temporary directory, which is returned by the Get. Temp. Directory function. name. Conflict = "behavior" Possible values: error, skip, overwrite, makeunique

cffile action = "upload" accept = "mime_type/file_type“ gives ablity to control what file types

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 - Cold. Fusion 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 Cold. Fusion, filenames were made unique by appending an additional "1" for each file, as in 1, 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" Example: FORM PAGE <form method="post" action="tag_cffile_upload. cfm" name="upload. Form" enctype="multipart/form-data">

cffile action = "upload" Example: FORM PAGE <form method="post" action="tag_cffile_upload. cfm" name="upload. Form" enctype="multipart/form-data"> <input name="File. Contents" type="file"> <input name="submit" type="submit" value="Upload File"> </form> POST PAGE <cffile action = "upload" file. Field = "File. Contents" destination = "c: filesupload" accept = "text/html" name. Conflict = "Make. Unique"> <input type="file"> automagically adds Browse button to form

append - append to existing file <cffile action = "append" file = "full_path_name" output

append - append to existing file <cffile action = "append" file = "full_path_name" output = “test string" add. New. Line = "Yes" or "No" attributes = "file_attributes_list" mode = "mode" charset = "charset_option" > addnewline = append newline character? can include html, cfml, javascript

copy – copy file on server <cffile action = "copy" source = "full_path_name" destination

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

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

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

move - move file on server <cffile action = "move" source = "c: filesuploadkeymemo.

move - move file on server <cffile action = "move" source = "c: filesuploadkeymemo. doc" destination = "c: filesmemo">

read - read file on server Note - reads entire file into memory This

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

read. Binary – read binary file <cffile action = "read. Binary" file = "full_path_name"

read. Binary – read binary file <cffile action = "read. Binary" file = "full_path_name" variable = "var_name"> Example: <cffile action = "read. Binary" file = "C: Inetpubwwwrootcfdocsgetting_startedphotossomewhere. jpg" variable = "a. Binary. Obj"> <!--- Output binary object to jpg format for viewing ---> <cffile action="write" file = "c: filesupdatessomewhere. B. jpg" output="#to. Binary(a. Binary. Obj)#"> <!--- HTML to view image ---> <img src="C: filesupdatessomewhere. B. jpg">

rename - rename a file <cffile action = "rename" source = "full_path_name" destination =

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

write - write to a file on server <cffile action = "write" file =

write - write to a file on server <cffile action = "write" file = "c: filesLovecraftcthulhu. txt" output = "In his house at R'lyeh dead Cthulhu waits dreaming" mode = "permission" add. New. Line = "Yes" or "No" attributes = "file_attributes_list" charset = "charset_option" >

cfdirectory <cfdirectory action = "directory action" directory = "directory name" name = "query name"

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

cfdirectory Actions: • list - returns a query record set of the files in

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, file 2 DESC, size, datelastmodified"

cfdirectory Cfdirectory returns the following fields in the query: • #mydirectory. name# • #mydirectory.

cfdirectory Cfdirectory returns the following fields in the query: • #mydirectory. name# • #mydirectory. size# • #mydirectory. type# • #mydirectory. date. Last. Modified# • #mydirectory. attributes# • #mydirectory. mode#

cfcontent <cfcontent type = "file_type" delete. File = "Yes" or "No" file = "filename"

cfcontent <cfcontent type = "file_type" delete. File = "Yes" or "No" file = "filename" reset = "Yes" or "No"> Example: <cfcontent type = "text/html" file = "c: inetpubwwwrootcfdocsmain. htm" delete. File = "No"> Useful for returning type xls, doc, html Browser will attempt to open using application Mime Types: application/excel application/msword

Related Functions Getting directory information on the server Get. Base. Template. Path() Get. Current.

Related Functions Getting directory information on the server Get. Base. Template. Path() Get. Current. Template. Path() Expandpath() Get. Directory. From. Path() example: <cfset this. Path=Expand. Path("*. *")> <cfset this. Directory=Get. Directory. From. Path(this. Path)> <cfoutput> The current full path is : #thispath# The current directory is: #Get. Directory. From. Path(this. Path)# </cfoutput>

Manipulating Windows registry - cfregistry action = "get. All" cfregistry action = "get" cfregistry

Manipulating Windows registry - cfregistry action = "get. All" cfregistry action = "get" cfregistry action = "set" cfregistry action = "delete"

Manipulating. ini files getprofilestring() setprofilestring()

Manipulating. ini files getprofilestring() setprofilestring()

CFMX XML Functions <cfxml> <Xml. Parse> <Xml. New> <Xml. Transform> complete list: http: //livedocs.

CFMX XML Functions <cfxml> <Xml. Parse> <Xml. New> <Xml. Transform> 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

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

Ouroboros

Ouroboros

Using the java. io library ouro. cfm <CFSET this. Path= Expand. Path("*. *")> <CFSET

Using the java. io library ouro. cfm <CFSET this. Path= Expand. Path("*. *")> <CFSET this. Directory= Get. Directory. From. Path(this. Path)> <cfset file. As. String = ""> <cfset file. To. Read = "#this. Directory#ouro. cfm"> <cfscript> file. Reader = create. Object("java", "java. io. File. Reader"); file. Reader. init(file. To. Read); buffered. Reader = create. Object("java", "java. io. Buffered. Reader"); buffered. Reader. init(file. Reader); try { do { file. As. String = buffered. Reader. read. Line(); process. Line(file. As. String); } while (true); } catch (coldfusion. runtime. Undefined. Variable. Exception e) { // this indicates end of file, ok to ignore error } </cfscript> <cffunction name="process. Line"> <cfargument name="line" required="true"> <cfoutput>#arguments. line# </cfoutput> </cffunction>

Other Uses for the Java. io library • Manipulating zip files • Image handling

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

Applications and Examples CFCDoc http: //www. spike. org. uk/projects/cfcdoc/ CFFM Demo: http: //www. webworksllc.

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

References Cffile http: //livedocs. macromedia. com/coldfusion/6. 1/htmldocs/tags-p 27. htm Multiple File Upload with cffile

References Cffile http: //livedocs. macromedia. com/coldfusion/6. 1/htmldocs/tags-p 27. 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? Topic. ID=96 http: //www. creative-restraint. co. uk/blog/ index. cfm? mode=entry&entry=E 3 EE 09 EF-CBC 6 -17 C 4 -075331 DBF 60 DCED 9 CFMX XML Functions http: //livedocs. macromedia. com/coldfusion/6. 1/htmldocs/xml. htm