0

I am struggling with the following cffile code. I am trying to pass a dynamically generated images directory, the name of which is stored in a session variable (one for each new member). I need to pass that name to the destination attribute of the cffile tag:

Heres my code for processing the file upload

<cfset mypath=expandpath('UserImages/UploadedImages/' & '#session.details.uimages#')>
<cffile action="upload" destination="#mypath#" nameconflict="makeunique"/>

The upload works fine if I replace the #session.details.uimages# with an actual directory name. Is, what I am trying to do, achieveable or is this not possible?

Any help, or guidance will be welcomed as I am tearing my hair out!

Thank you all in advance!

Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
Paresh
  • 11
  • 3

1 Answers1

6

If the directory doesn't exist, you need to create it first.

If uimages comes from the user, don't forget to first verify it is a valid directory name (and specifically, that it doesn't contain path traversal syntax, i.e. ..).

Also, your cffile lacked the filefield attribute, which is required.

Note as well the lack of hashes around the session variable - they are unnecessary.

<cfset MyPath = expandPath( 'UserImages/UploadedImages/' & session.details.uimages ) />

<cfif NOT DirectoryExists( MyPath )>
    <cfset DirectoryCreate( MyPath ) />
</cfif>

<cffile
    action       = "upload"
    destination  = "#MyPath#"
    nameconflict = "makeunique"
    filefield    = "name_of_field_to_upload_from"
/>
Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
  • Thank you for yur reply Peter. The code above has been implemented. It still doesn't work. I have been following Ramond Camden's blog at http://www.raymondcamden.com/index.cfm/2009/11/11/Important-notes-about-ColdFusion-9s-new-multi-file-uploader – Paresh Aug 26 '12 at 09:49
  • "It still doesn't work" is meaningless - _what_ doesn't work and _how_? i.e. What is the error message or unexpected result? (Also, when you're following a blog post, state that in the original question, along with which version of CF you are using.) – Peter Boughton Aug 26 '12 at 13:00
  • Ray has a newer/complete tutorial for this here: http://www.raymondcamden.com/index.cfm/2010/3/5/ColdFusion-9-Multifile-Uploader--Complete-Example – Peter Boughton Aug 26 '12 at 13:05
  • Sorry for hiccup. I am using CF10 (hosted). 'Doesn't work' means selected file isn't uploaded. Code works by replacing session variable to a static name. This code is called from a page with this code: Destination attr of cffile on processing page, uploadall.cfm doesn't like variables passed to it from outside that page. If a new variable name is created on processing page, the code works fine. I wonder its something to do with tracking the session variables across pages! Peter I am new to this so please bear with me and advise accordingly. – Paresh Aug 26 '12 at 19:57