Skip to content

Instantly share code, notes, and snippets.

@pmcelhaney
Created August 9, 2010 17:42
Show Gist options
  • Save pmcelhaney/515771 to your computer and use it in GitHub Desktop.
Save pmcelhaney/515771 to your computer and use it in GitHub Desktop.
Creates a directory on an FTP server if it doesn't already exist. If necessary, creates intermediate directories as well.
<!---
Creates a directory on an FTP server if it doesn't already exist. If necessary,
creates intermediate directories as well.
Usage:
<cfftp action="open" connection="conn" ... />
<cfset ensureFTPDirectoryExists("/path/to/a/dir", conn)>
--->
<cffunction name="ensureFTPDirectoryExists">
<cfargument name="dir"/>
<cfargument name="connection"/>
<cfset var cfftpResult = 0 />
<cfset var exists = false />
<cfset var parentDirectory = "" />
<cftry>
<cfftp
connection="#connection#"
action="existsDir"
directory="#dir#"
result="cfftpResult"/>
<cfset exists = cfftpResult.returnValue>
<cfcatch type="Application">
<cfif cfcatch.Message IS "An error occurred during the sFTP EXISTSDIR operation.">
<cfset exists = false />
<cfelse>
<cfrethrow />
</cfif>
</cfcatch>
</cftry>
<cfif exists eq FALSE>
<cfset parentDirectory = reverse(listRest(reverse(dir), '/'))/>
<cfset ensureFTPDirectoryExists( parentDirectory , connection)/>
<cfftp
connection="#connection#"
action="createDir"
directory="#dir#"/>
</cfif>
</cffunction>
@naniterrific
Copy link

thanks its helpful but is it the wrong way to create directory or its safe to use??

@wassadude
Copy link

Thanks nice bit of code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment