Skip to main content
July 30, 2010
Answered

creating a directory before using cffile

  • July 30, 2010
  • 4 replies
  • 4898 views

I'm having trouble using cffile right before I create a directory. I am using the cffileupload tag and my url attribute is a page that has the following code. Basically the code below creates a new directory and uploads all the images to that directory. However, it fails on the 2nd upload and i get a 500 error in the cffileupload flash object. However, if I hardcode the directory path, they all upload fine. Anyone know why I am having this problem?

<!--- User will upload all the images to a temp directory based on date and time --->

<cfset uploadFolderPath = "C:\ColdFusion9\wwwroot\MyApplication\uploads\" />

<cfset date=DateFormat(Now(),'mm-dd-yyyy_') />

<cfset time=TimeFormat(Now(),'hh-mm-ss') />

<cfset newFolderName = "upload_" & date & time />

<cfset newFolder = uploadFolderPath & newFolderName />

<cfdirectory action = "create" directory="#newFolder#" />

<cffile action="uploadall" destination="#newFolder#" nameconflict="makeunique" />

    This topic has been closed for replies.
    Correct answer Adam Cameron.

    It seems unlikely to be your problem, but be aware that <cffileupload> calls that URL once for each file uploaded, not just once.  That means the <cfdirectory> could conceivably be called twice within the same second, meaning the second call to it might fail because the dir already exists.

    You should probably check the dir exists before just assuming you're OK to create it.

    --

    Adam

    4 replies

    August 2, 2010

    I didn't know the destination path in the url attribute of cffileupload is called for each file. I checked to see if the directory exists before creating it using the code JR posted and it solved the problem. I guess it was trying to create a directory that was already created during the same second for the previous file being uploaded. Thanks for the help guys!

    Inspiring
    August 2, 2010

    Is it possible that <cfdirectory action = "create" directory="#newFolder#" /> is invoked twice?  The problem being that newFolder cannot be created because it already exists.

    You might try something like this:

    <cfif Not DirectoryExists(newFolder)>
        <cfdirectory action = "create" directory="#newFolder#" />
    </cfif>

    Adam Cameron.Correct answer
    Inspiring
    July 31, 2010

    It seems unlikely to be your problem, but be aware that <cffileupload> calls that URL once for each file uploaded, not just once.  That means the <cfdirectory> could conceivably be called twice within the same second, meaning the second call to it might fail because the dir already exists.

    You should probably check the dir exists before just assuming you're OK to create it.

    --

    Adam

    Inspiring
    July 30, 2010

    If there are no other error details, check the logs for the full error message, or alternately trap any errors with a cftry/cfcatch and save them to a file.