1

I'm pulling my hair our here and really can't figure out why this code will not work. I need to use <cffile action="write"> to put a document on the server.

I've stripped the code to its bare minimum in an attempt to get the file to write to the server, however it still won't budge. The code I am running is this.

<cffile action = "write" 
        file = "test.txt"
        output = "Content"
>

When I run this code on the server, it does nothing. I get no error messages on-screen, however no file appears on the server either.

I've been searching for a while and the only thing I could seem to come up with was writing the absolute file path, so I've also tried this too

<cffile action = "write" 
        file = "http://www.my_url.com/test.txt"
        output = "Content"
>

Which does get me an error message (see below), however searches for how to sort this error message have been less than useful.

An error occurred when performing a file operation write on file 
http://www.my_url.com/test.txt.
The cause of this exception was: java.io.FileNotFoundException:  
http://www.my_url.com/test.txt.

I thought this could be a problem with my privileges, however there is no 'access is denied' error on the end of the FileNotFound, so I really am lost as to what to do.

It's doubly infuriating because on this exact site I am also using <cffile action="upload"> which works absolutely fine!

  • It may be writing the file to a directory you are not expecting ie a temporary directory. Use an absolute *physical* path ie `c:/path/to/test.txt`. Then check it with FileExists. – Leigh Aug 23 '12 at 14:15
  • File path needs to be an path on the server. Use #ExpandPath( './' )# or something similar to put it in your current working directory. Right now it's either going into temp or possibly the server root. – Busches Aug 23 '12 at 14:18

1 Answers1

5

As mentioned above, cffile only operates on the server file system. So you need to use a physical file path, not a url. Also, the documentation says if you use a relative path the file is written to:

"...(a path) relative to the ColdFusion temporary directory, which is returned by the GetTempDirectory function."

So if you did not receive an error, the file was created, just not where you expected. To avoid this kind of confusion, use absolute paths:

    <cffile action="write" file="c:/path/to/test.txt" output="Content">
Leigh
  • 28,765
  • 10
  • 55
  • 103