Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

creating a directory before using cffile

Guest
Jul 30, 2010 Jul 30, 2010

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" />

4.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 31, 2010 Jul 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

Translate
Valorous Hero ,
Jul 30, 2010 Jul 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 31, 2010 Jul 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Aug 02, 2010 Aug 02, 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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 02, 2010 Aug 02, 2010
LATEST

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources