Using ColdFusion to Manipulate Files and Directories James

Download Report

Transcript Using ColdFusion to Manipulate Files and Directories James

Using ColdFusion to Manipulate
Files and Directories
James Collins / AlphaInsight
MDCFUG Dec 14, 2004
[email protected]
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 (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
Basic ColdFusion file functions
• cffile
• cfdirectory
• cfcontent
cffile
Actions of the cffile tag:
•Upload
•Append
•Copy
•Delete
•Move
•Read
•readBinary
•Rename
•Write
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">
cffile action = "upload"
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 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="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
append - append to existing file
<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
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">
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 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" >
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">
<!--- 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
<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 = "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
<cfdirectory
action = "directory action"
directory = "directory name"
name = "query name"
filter = "list filter"
mode = "permission"
sort = "sort specification"
newDirectory = "new directory name">
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"
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, html
Browser will attempt to open using application
Mime Types:
application/excel
application/msword
Related Functions
Getting directory information on the server
GetBaseTemplatePath()
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
cfregistry
cfregistry action = "getAll"
cfregistry action = "get"
cfregistry action = "set"
cfregistry action = "delete"
Manipulating .ini files
getprofilestring()
setprofilestring()
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
Ouroboros
Using the java.io library
ouro.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>
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.com/cffm/
Download: http://cfopen.org/projects/cffm
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