如何确定ColdFusion中的文件路径是绝对路径还是相对路径
我有一个接受文件路径的函数。用户可以传入文件的绝对路径或相对路径。如果提供了相对路径,如何确定ColdFusion中的文件路径是绝对路径还是相对路径,coldfusion,Coldfusion,我有一个接受文件路径的函数。用户可以传入文件的绝对路径或相对路径。如果提供了相对路径,ExpandPath函数可以将其转换为绝对路径,如下所示: <cfset filepath = ExpandPath("data/test.txt") > 但如果用户提供了一个绝对路径,如: <cfset filepath = ExpandPath("C:\www\example\data\test") > 如何解决此问题?您可以测试字符串,看看它是否以C:\for windows或
ExpandPath
函数可以将其转换为绝对路径,如下所示:
<cfset filepath = ExpandPath("data/test.txt") >
但如果用户提供了一个绝对路径,如:
<cfset filepath = ExpandPath("C:\www\example\data\test") >
如何解决此问题?您可以测试字符串,看看它是否以C:\for windows或\\for unix开头,并将其用作if? 这可能是您的windows检查:
<cfif reFindNoCase("[a-zA-Z]:\\",myFileLocation)>
<!--- Is a absolute path --->
<cfelse>
<!--- Is not an absolute path --->
</cfif>
一种可能更灵活的方法是检查原始输入中的目录是否存在,如果不存在,请尝试expandpath。大概是这样的:
<cfif directoryExists(myFileLocation)>
<cfset theDirectory=myFileLocation)>
<cfelseif directoryExists(expandPath(myFileLocation))>
<cfset theDirectory=expandPath(myFileLocation)>
<cfelse>
<cfthrow message="Invalid directory!">
</cfif>
如果要执行reFindNoCase,则不需要在正则表达式中同时指定这两种情况!此外,在Windows上为CF提供C://
也是非常有效的。因此,如果这样做,您可能希望使用[a-z]:[\\/]
。(尽管我更倾向于使用Al的方法)我相信现代CFML现在允许在directoryExists()
中使用相对路径。
<cfif reFindNoCase("[a-zA-Z]:\\",myFileLocation)>
<!--- Is a absolute path --->
<cfelse>
<!--- Is not an absolute path --->
</cfif>
<cfif directoryExists(myFileLocation)>
<cfset theDirectory=myFileLocation)>
<cfelseif directoryExists(expandPath(myFileLocation))>
<cfset theDirectory=expandPath(myFileLocation)>
<cfelse>
<cfthrow message="Invalid directory!">
</cfif>