Using ColdFusion to Manipulate Files and Directories James Collins AlphaInsight MDCFUG Dec 14, 2004 - PowerPoint PPT Presentation

About This Presentation
Title:

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

Description:

uploading Word documents and Excel spreadsheets. Jobs site - uploading resumes ... Ouroboros. Using the java.io library. ouro.cfm CFSET thisPath= ExpandPath ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 32
Provided by: Colli56
Category:

less

Transcript and Presenter's Notes

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


1
Using ColdFusion to Manipulate Files and
Directories James Collins / AlphaInsight MDCFUG
Dec 14, 2004 jimcollins_at_gmail.com
2
Presentation Overview
  • Basic CF File Functions
  • Related File Functions
  • Using Java for File Handling
  • Applications and Examples
  • References

3
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

4
Basic ColdFusion file functions
  • cffile
  • cfdirectory
  • cfcontent

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

6
cffile action "upload"
syntax ltcffile action "upload"
fileField "formfield" destination
"full_path_name" nameConflict "behavior"
accept "mime_type/file_type" mode
"permission" attributes "file_attribute_or_li
st"gt
7
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
8
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"gt W
indows 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.
9
cffile action "upload"
Example FORM PAGE ltform method"post"
action"tag_cffile_upload.cfm" name"uploadForm"
enctype"multipart/form-data"gt ltinput
name"FileContents" type"file"gt ltbrgt ltinput
name"submit" type"submit" value"Upload File"gt
lt/formgt POST PAGE ltcffile action "upload"
fileField "FileContents" destination
"c\files\upload\" accept "text/html"
nameConflict "MakeUnique"gt ltinput
type"file"gt automagically adds Browse button to
form
10
append - append to existing file
ltcffile action "append" file
"full_path_name" output test string"
addNewLine "Yes" or "No" attributes
"file_attributes_list" mode "mode"
charset "charset_option" gt addnewline
append newline character? can include html,
cfml, javascript
11
copy copy file on server
ltcffile action "copy" source
"full_path_name" destination
"full_path_name" mode "mode" attributes
"file_attributes_list"gt
12
delete - delete file on server
ltcffile action "delete" file
"full_path_name"gt
13
move - move file on server
ltcffile action "move" source
"c\files\upload\keymemo.doc" destination
"c\files\memo\"gt
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 ltcffile action "read" file
"full_path_name" variable "var_name" charset
"charset_option" gt
15
readBinary read binary file
ltcffile action "readBinary" file
"full_path_name" variable "var_name"gt Example
ltcffile action "readBinary" file
"C\Inetpub\wwwroot\cfdocs\getting_started\photos\
somewhere.jpg" variable "aBinaryObj"gt lt!---
Output binary object to jpg format for viewing
---gt ltcffile action"write" file
"c\files\updates\somewhereB.jpg"
output"toBinary(aBinaryObj)"gt lt!--- HTML to
view image ---gt ltimg src"C\files\updates\somewhe
reB.jpg"gt
16
rename - rename a file
ltcffile action "rename" source
"full_path_name" destination "path_name" mode
"mode" attributes "file_attributes_list"gt If
destination is a directory the file is only
moved and not renamed
17
write - write to a file on server
ltcffile 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" charse
t "charset_option" gt
18
cfdirectory
ltcfdirectory action "directory
action" directory "directory name" name
"query name" filter "list filter" mode
"permission" sort "sort specification" newDirect
ory "new directory name"gt
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
ltcfcontent type "file_type" deleteFile
"Yes" or "No" file "filename" reset "Yes"
or "No"gt Example ltcfcontent type "text/html"
file "c\inetpub\wwwroot\cfdocs\main.htm"
deleteFile "No"gt Useful for returning type
xls, doc, html Browser will attempt to open using
application Mime Types application/excel applic
ation/msword
22
Related Functions
Getting directory information on the
server GetBaseTemplatePath() GetCurrentTemplatePat
h() Expandpath() GetDirectoryFromPath() example
ltcfset thisPathExpandPath(".")gt ltcfset
thisDirectoryGetDirectoryFromPath(thisPath)gt ltcfo
utputgt The current full path is thispath ltbr
/gt The current directory is GetDirectoryFromPath
(thisPath) ltbr /gt lt/cfoutputgt
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
ltcfxmlgt ltXmlParsegt ltXmlNewgt ltXmlTransformgt comple
te 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 ltCFSET thisPath ExpandPath(".")gt
ltCFSET thisDirectory GetDirectoryFromPath(thisPa
th)gt ltcfset fileAsString ""gt ltcfset fileToRead
"thisDirectoryouro.cfm"gt ltcfscriptgt fileReader
createObject("java", "java.io.FileReader") fil
eReader.init(fileToRead) bufferedReader
createObject("java", "java.io.BufferedReader") bu
fferedReader.init(fileReader) try do
fileAsString bufferedReader.readLine()
processLine(fileAsString) while (true)
catch (coldfusion.runtime.UndefinedVariableExcepti
on e) // this indicates end of file, ok to
ignore error lt/cfscriptgt ltcffunction
name"processLine"gt ltcfargument name"line"
required"true"gt ltcfoutputgtarguments.lineltbr
/gtlt/cfoutputgt lt/cffunctiongt
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/ CF
FM 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-Fu
sion/Multiple-File-Upload-with-CFFILE/ Java file
functions http//www.johnwbartlett.com/cf_tipsntri
cks/index.cfm?TopicID96 http//www.creative-rest
raint.co.uk/blog/ index.cfm?modeentryentryE3EE0
9EF-CBC6-17C4-075331DBF60DCED9 CFMX XML
Functions http//livedocs.macromedia.com/coldfusio
n/6.1/htmldocs/xml.htm
Write a Comment
User Comments (0)
About PowerShow.com