I have a function that accepts a file path. Users can pass in either an absolute or relative path to a file. If a relative path is provided, the ExpandPath
function can convert it to an absolute path like so:
<cfset filepath = ExpandPath("data/test.txt") >
.. and it returns:
C:\www\example\data\test
But if user provides an absolute path like:
<cfset filepath = ExpandPath("C:\www\example\data\test") >
.. it returns:
C:\www\example\C:\www\example\data\test
How may I solve this problem?
A possibly more flexible way to do this is to check to see if the directory from the raw input exists and, if not, try expandpath. Something like this:
<cfif directoryExists(myFileLocation)>
<cfset theDirectory=myFileLocation)>
<cfelseif directoryExists(expandPath(myFileLocation))>
<cfset theDirectory=expandPath(myFileLocation)>
<cfelse>
<cfthrow message="Invalid directory!">
</cfif>
You could test the string and see if it starts with C:\ for windows or \\ for unix, and use that as an if? This could be your windows check:
<cfif reFindNoCase("[a-zA-Z]:\\",myFileLocation)>
<!--- Is a absolute path --->
<cfelse>
<!--- Is not an absolute path --->
</cfif>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With