How to Get Uploaded Filename in Coldfusion

ColdFusion ix.0 Resources

  • Configuring and Administering Adobe ColdFusion ix

  • Adobe ColdFusion 9 CFML Reference

  • Developing Adobe ColdFusion 9 Applications

  • Installing Adobe ColdFusion 9

Using cffile


Contents [Hide]



You can use the cffile tag to piece of work with files on the server in several ways:

  • Upload files from a client to the web server using an HTML form

  • Move, rename, re-create, or delete files on the server

  • Read, write, or suspend to text files on the server

You lot utilise the activeness attribute to specify any of the post-obit file actions: upload, move, rename, copy, delete, read, readBinary, write, and append. The required attributes depend on the action specified. For example, if action="write", ColdFusion expects the attributes associated with writing a text file.

Note: Consider the security and logical structure of directories on the server before allowing users access to them. You lot can disable the cffile tag in the ColdFusion Administrator. Too, to admission files that are not located on the local ColdFusion system, ColdFusion services must run using an account with permission to admission the remote files and directories.

Uploading files

File uploading requires that you lot create two files:

  • An HTML form to specify file upload data

  • An activity page containing the file upload code

The following procedures describe how to create these files.

Create an HTML file to specify file upload information

  1. Create a ColdFusion page with the following content:

    <head><title>Specify File to Upload</championship></head>  <trunk>  <h2>Specify File to Upload</h2>  <!--- the activity attribute is the name of the activeness page --->  <form activity="uploadfileaction.cfm"           enctype="multipart/form-data"           method="post">      <p>Enter the complete path and filename of the file to upload:      <input type="file"              name="FiletoUpload"              size="45">      </p>      <input     type="submit"              value="Upload">  </form>  </body>
  2. Salvage the file equally uploadfileform.cfm in the myapps directory nether your web_root and view it in the browser.

Annotation: The form does non work until you write an action folio for it (run across the next procedure).

Reviewing the code

The following tabular array describes the code and its function:

Code

Description

<form action="uploadfileaction.cfm" enctype="multipart/course-data" method="post">

Create a class that contains file option fields for upload past the user. The action attribute value specifies the ColdFusion template that processes the submitted form. The enctype attribute value tells the server that the grade submission contains an uploaded file. The method attribute is set up to post to submit a ColdFusion form.

<input type="file" proper name="FiletoUpload" size="45">

Allow the user to specify the file to upload. The file type instructs the browser to prepare to read and transmit a file from the user organisation to your server. Information technology automatically includes a Scan push to allow the user look for the file instead of manually entering the unabridged path and filename.

The user can enter a file path or scan the system and select a file to transport.

  1. Create a ColdFusion page with the following content:

    <html>  <head> <title>Upload File</championship> </caput>  <torso>  <h2>Upload File</h2>    <cffile action="upload"          destination="c:\temp\"          nameConflict="overwrite"          fileField="Class.FiletoUpload">            <cfoutput>  Yous uploaded #cffile.ClientFileName#.#cffile.ClientFileExt#              successfully to #cffile.ServerDirectory#.  </cfoutput>    </body>  </html>
  2. Change the post-obit line to point to an appropriate location on your server:

    destination="c:\temp\"

    Annotation: This directory must be on the server.

  3. Salve the file as uploadfileaction.cfm in the myapps directory nether your web_root.

  4. View uploadfileform.cfm in the browser, enter a file to upload, and submit the form.

    The file you specified uploads.

Reviewing the lawmaking

The post-obit table describes the lawmaking and its role:

Lawmaking

Description

<cffile action="upload"

Output the proper name and location of the uploaded file on the client automobile.

destination="c:\temp\"

Specify the destination of the file.

nameConflict="overwrite"

If the file exists, overwrite it.

fileField="Grade.FiletoUpload">

Specify the name of the file to upload. Do not enclose the variable in number signs.

You uploaded #cffile.ClientFileName#.#cffile.  ClientFileExt# successfully to  #cffile.ServerDirectory#.

Inform the user of the file that was uploaded and its destination. For information on scope variables, see Evaluating the results of a file upload.

Note: This example performs no error checking and does not incorporate any security measures. Before deploying an application that performs file uploads, ensure that you lot incorporate both error treatment and security. For more information, run across Securing Applications and Treatment Errors.

Resolving conflicting filenames

When you relieve a file to the server, a file with the same name could exist. To resolve this problem, assign one of these values to the nameConflict attribute of the cffile tag:

Fault
(default) ColdFusion stops processing the folio and returns an error. The file is not saved.
Skip
Allows custom behavior based on file backdrop. The tag does not save the file or return an error.
Overwrite
Overwrites a file that has the same name equally the uploaded file.
MakeUnique
Generates a unique filename for the uploaded file. The name is stored in the file object variables serverFile and serverFileName. Yous tin can use this variable to record the proper name used when the file was saved. The unique proper noun might not resemble the attempted proper noun. For more data on file upload status variables, see Evaluating the results of a file upload.

Decision-making the type of file uploaded

For some applications, you could want to restrict the blazon of file that is uploaded, for case, to not take graphic files in a document library.

Yous use the accept attribute to restrict the type of file that you allow in an upload. When an accept qualifier is present, the uploaded file MIME content blazon must match the criteria specified or an error occurs. The accept aspect takes a comma-separated list of MIME data names, optionally with wildcards.

The browser determines the file MIME blazon. Common types, such as prototype/gif and text/obviously, are registered in the browser.

Note: Electric current versions of Microsoft Internet Explorer and Netscape support MIME blazon associations. Other browsers and earlier versions might ignore these associations.

ColdFusion saves any uploaded file if you omit the accept attribute or specify "*/*". You lot can restrict the file types, every bit demonstrated in the following examples.

The following cffile tag saves an image file merely if it is in the GIF format:

<cffile activeness="Upload"      fileField="Class.FiletoUpload"      destination="c:\uploads\"      nameConflict="Overwrite"      accept="image/gif">

The following cffile tag saves an image file but if information technology is in GIF or JPEG format:

<cffile action="Upload"      fileField="Form.FiletoUpload"      destination="c:\uploads\"      nameConflict="Overwrite"      have="image/gif, image/jpeg">                        

Annotation: If you receive an error like "The MIME blazon of the uploaded file (image/jpeg) was not accepted by the server", enter accept="image/jpeg" to have JPEG files.

This cffile tag saves any image file, regardless of the format:

<cffile action="Upload"      fileField="Grade.FiletoUpload"      destination="c:\uploads\"      nameConflict="Overwrite"      have="image/*">

Setting file and directory attributes

In Windows, you specify file attributes using attributes attribute of the cffile tag. In UNIX, you specify file or directory permissions using the mode attribute of the cffile or cfdirectory tag.

Windows

In Windows, you tin ready the following file attributes:

  • Hidden

  • Normal

  • ReadOnly

To specify several attributes in CFML, utilize a comma-separated list for the attributes attribute; for example, attributes="ReadOnly,Subconscious". If you lot do non use the attributes aspect, the existing attributes of the file are maintained. If yous specify any other attributes in addition to Normal, the additional aspect overrides the Normal setting.

UNIX

In UNIX, you can individually fix permissions on files and directories for each of three types of users—possessor, group, and other. You use a number for each user type. This number is the sum of the numbers for the private permissions allowed. Values for the manner attribute correspond to octal values for the UNIX chmod control:

  • 4 = read

  • 2 = write

  • 1 = execute

You enter permissions values in the way attribute for each blazon of user: owner, grouping, and other in that order. For instance, use the following lawmaking to assign read permissions for anybody:

mode=444

To give a file or directory possessor read/write/execute permissions and read-only permissions for anybody else:

mode=744

Evaluating the results of a file upload

After a file upload is completed, you can call back status information using file upload status variables. This status information includes information about the file, such as its proper name and the directory where information technology was saved.

You can access file upload status variables using dot note, using either file.varname or cffile.varname. Although you can use either the File or cffile prefix for file upload condition variables, cffile is preferred; for example, cffile.ClientDirectory. The File prefix is retained for astern compatibility.

Note: File condition variables are read only. They are gear up to the results of the most contempo cffile functioning. If two cffile tags execute, the results of the get-go are overwritten by the subsequent cffile functioning.

The post-obit table describes the file upload status variables that are available later on an upload:

Variable

Description

attemptedServerFile

Initial name that ColdFusion uses when attempting to salvage a file; for example, myfile.txt. (meet Resolving conflicting filenames).

clientDirectory

Directory on the client system from which the file was uploaded.

clientFile

Full proper name of the source file on the client system with the filename extension; for case, myfile.txt.

clientFileExt

Extension of the source file on the client system without a period; for example, txt (not .txt).

clientFileName

Proper name of the source file on the client organisation without an extension; for example, myfile.

contentType

MIME content blazon of the saved file; for example, prototype for image/gif.

contentSubType

MIME content subtype of the saved file; for example, gif for image/gif.

dateLastAccessed

Appointment that the uploaded file was last accessed.

fileExisted

Indicates (Yep or No) whether the file existed with the same path.

fileSize

Size of the uploaded file.

fileWasAppended

Indicates (Yeah or No) whether ColdFusion appended the uploaded file to an existing file.

fileWasOverwritten

Indicates (Yes or No) whether ColdFusion overwrote a file.

fileWasRenamed

Indicates (Yep or No) whether the uploaded file was renamed to avert a name conflict.

fileWasSaved

Indicates (Yep or No) whether ColdFusion saved the uploaded file.

oldFileSize

Size of the file that was overwritten in the file upload operation. Empty if no file was overwritten.

serverDirectory

Directory where the file was saved on the server.

serverFile

Total proper name of the file saved on the server with the filename extension; for example, myfile.txt.

serverFileExt

Extension of the file saved on the server without a period; for case, txt (non .txt).

serverFileName

Name of the file saved on the server without an extension; for example, myfile.

timeCreated

Appointment and time the uploaded file was created.

timeLastModified

Engagement and time of the last modification to the uploaded file.

Moving, renaming, copying, and deleting server files

With the cffile tag, you can create awarding pages to manage files on your web server. You lot can apply the tag to move files from ane directory to another, rename files, copy a file, or delete a file.

The examples in the following table show static values for many of the attributes. Nevertheless, the value of all or role of whatever attribute in a cffile tag can be a dynamic parameter.

Action

Example code

Move a file

<cffile activity="move" source="c:\files\upload\KeyMemo.doc" destination="c:\files\memo\">

Rename a file

<cffile action="rename" source="c:\files\memo\KeyMemo.physician" destination="c:\files\memo\OldMemo.doc">

Copy a file

<cffile action="re-create" source="c:\files\upload\KeyMemo.doc" destination="c:\files\backup\">

Delete a file

<cffile activeness="delete" file="c:\files\upload\oldfile.txt">

This instance sets the ReadOnly flag fleck for the uploaded file:

<cffile action="Copy"      source="c:\files\upload\keymemo.doc"      destination="c:\files\fill-in\"      attributes="ReadOnly">

Notation: Ensure that you include the trailing slash (\) when you specify the destination directory. Otherwise, ColdFusion treats the last chemical element in the path as a filename. This dominion only applies to copy deportment.

Reading, writing, and appending to a text file

In improver to managing files on the server, you tin can use the cffile tag to read, create, and modify text files. As a effect, you can do the following things:

  • Create log files. (Y'all can likewise use cflog to create and write to log files.)

  • Generate static HTML documents.

  • Use text files to shop information that can be incorporated into web pages.

Reading a text file

You can apply the cffile tag to read an existing text file. The file is read into a local variable that y'all tin can utilise anywhere in the application folio. For example, you lot could read a text file and then insert its contents into a database, or yous could read a text file and and then use one of the string replacement functions to change the contents.

Read a text file

  1. Create a ColdFusion folio with the following content:

    <html>  <head>      <title>Read a Text File</title>  </head>    <body>  Ready to read the file:<br>  <cffile action="read"      file="C:\inetpub\wwwroot\mine\bulletin.txt"      variable="Message">    <cfoutput>      #Message#  </cfoutput>  </body>  </html>
  2. Supervene upon C:\inetpub\wwwroot\mine\message.txt with the location and name of a text file on the server.

  3. Relieve the file equally readtext.cfm in the myapps directory under your web_root and view it in the browser.

Writing a text file on the server

You can utilize the cffile tag to write a text file based on dynamic content. For example, you could create static HTML files or log actions in a text file.

Create a form in to capture data for a text file

  1. Create a ColdFusion page with the following content:

    <html>  <head>      <title>Put Information into a Text File</title>  </head>    <body>  <h2>Put Information into a Text File</h2>    <form activity="writetextfileaction.cfm" method="Mail">      <p>Enter your proper noun: <input blazon="text" proper name="Proper name" size="25"></p>      <p>Enter the name of the file: <input type="text" name="FileName" size="25">.txt</p>      <p>Enter your message:      <textarea name="bulletin"cols=45 rows=half dozen></textarea>      </p>      <input type="submit" proper name="submit" value="Submit">   </form>  </trunk>  </html>
  2. Save the file as writetextfileform.cfm in the myapps directory under your web_root .

Note: The course does not piece of work until you write an action folio for it (see the next procedure).

Write a text file

  1. Create a ColdFusion page with the following content:

    <html>  <head>      <title>Write a Text File</title>  </head>  <body>  <cffile action="write"      file="C:\inetpub\wwwroot\mine\#Form.FileName#.txt"      output="Created By: #Form.Proper noun#  #Form.Message# ">  </body>  </html>
  2. Change the path C:\inetpub\wwwroot\mine\ to signal to a path on your server.

  3. Salve the file as writetextfileaction.cfm in the myapps directory under your web_root.

  4. View the file writetextfileform.cfm in the browser, enter values, and submit the form.

    The text file is written to the location you specified. If the file exists, it is replaced.

Appending a text file

You lot tin can use the cffile tag to append additional text to the stop of a text file; for example, when you lot create log files.

Append a text file

  1. Open up the writetextfileaction.cfm file.

  2. Change the value for the activeness attribute from write to suspend and so that the file appears as follows:

    <html>  <head>      <title>Append a Text File</title>  </head>  <torso>  <cffile activeness="append"      file="C:\inetpub\wwwroot\mine\message.txt"      output="Appended By: #Form.Name#">  </trunk>  </html>
  3. Save the file as writetextfileaction.cfm in the myapps directory under your web_root.

  4. View the file in the browser, enter values, and submit the class.

    The appended information displays at the end of the text file.

jordangurn1970.blogspot.com

Source: https://www.3gpp2.org/cfdocs/htmldocs/Developing/WSc3ff6d0ea77859461172e0811cbec1553c-7fe9.html

0 Response to "How to Get Uploaded Filename in Coldfusion"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel