Skip to main content
2Charlie
Inspiring
March 11, 2016
Answered

Is CFFILE read uses only absolute path or relative path too?

  • March 11, 2016
  • 1 reply
  • 1019 views

I'm using cffile read to access to my json file. I like to know if cffile only uses absolute path or relative too? If I tried to use relative path, it kept giving me errors about file not exist.

    This topic has been closed for replies.
    Correct answer haxtbh

    Okay, that works but it's not "technically" relative path. It's still using absolutely path. So back the question, if cffile read is able to use relative path, in ColdFusion, what is it "relative" to? By the way, many thanks for that code. It's much better than what I had.


    Read what I posted about the documentation. cffile read has to be an absolute path. If you use a relative path its relative to the CF temp folder:

    If not an absolute path (starting with a drive letter and a colon, or a forward or backward slash), it is relative to the ColdFusion temporary directory, which is returned by the GetTempDirectory function.

    There is no relative path relative to your web files. IF you just put a relative path in, it will be looking in CFs temp folder for the file.

    1 reply

    Inspiring
    March 11, 2016

    As per the docs- ColdFusion Help | cffile action = "read"

    Pathname of the file to read.
    If not an absolute path (starting with a drive letter and a colon, or a forward or backward slash), it is relative to the ColdFusion temporary directory, which is returned by theGetTempDirectory function.

    If you want to use a relative path you can use ColdFusion Help | ExpandPath to pass the relative path and get the absolute path.

    2Charlie
    2CharlieAuthor
    Inspiring
    March 11, 2016

    Thanks for the help. This is the code I have that is working.

    <cfset groupData = deserializeJSON(fileread("\\commonspotshare.mysite.com\commonspot$\DEVELOPMENT\devbox.mysite.com\kb\article\Groups.json")) />

    However, I believed this is an absolute path. I've tried the following and it's not working. The location of the display page is https://devbox.mysite.com/kb/article/index.cfm?title=test-article and the script that runs in the background to query the content are in the https://devbox.mysite.com/customcf/knowledge/knowledge.cfm but the Groups.json is the https://devbox.mysite.com/kb/article/ folder. So, that is why I'm confused as to "relative" to what. The path relative to the display page, knowledge.cfm script page, or the actual Groups.json file? I tried the following with relative to the actual groups.json file and display or index.cfm page but it's not working.

    <cfset groupData = deserializeJSON(fileread("Groups.json")) />

    Got this error for the above line.

    The cause of this exception was: java.io.FileNotFoundException.

    Inspiring
    March 11, 2016

    You can just do something like:

    <cfset absPath = expandPath("/kb/article/Groups.json")>

    <cfset groupData = deserializeJSON(fileread(absPath))