Wednesday, February 22, 2012

File Write Operation on a Shared Path on Different Computer in ColdFusion

  Generally file operation in ColdFusion is very simple, we only need a ColdFusion tag "CFFILE" to do the file operation like read,write,append and delete etc.

To make a simple file write Operation in ColdFusion we will write the code as follows:

<cffile action="write" file="D:/test.txt" output="Testing" />

To write this file(test.txt) in some shared directory like "\\\Photos\", we will write the code as :

<cffile action="write" file="\\\Photos\text.txt" output="Testing">

But, this code will give an error message:

"An error occured when performaing a file operation write on file \\\test.txt.
The cause of this exception was: \\\Photos\test.txt (Access is denied)."


After getting this message I tried by creating "Map Network Drive" and also by creating a "Map Network Location" but all the testing was in vein.

The main Cause of the Error:
         The ColdFusion application doesn't have enough permission to access the shared directory. But, the user by which you have login to the system have rights to access the shared directory.
How we will give Permission to Our ColdFusion Server to Access the Network Shared Directory:

           The ColdFusion runs in Windows as a service and if we look ino all the services of the machine we will find as follow:

Here, you can notice that for ColdFusion service the LogOnAs value is "LocalSyatem". It means ColdFusion service is started as "LocalSystem" login and this login doesn't have enough permission to do any operation on network. So, in order to make the required file operation in the Shared Directory we will have to change the LogOnAs value for ColdFusion service.

How we will change the LogOnAs value for ColdFusion(or any window service):
  1. Right Click on the ColdFusion Service then click on "properties".
  2. Click on "Log On" tab. Then you will see a screen like this:

  1. Select "This account" radio button .Then click on "Browse" button and select the user by which you want to start the service. You should choose the user who have sufficient permission to create file on the shared directory and also have required permission in local.
  2. After the set up you can see the effect as follow(sample example): 
Here, you can notice the change in the "Log On As" value. But we have to restart the ColdFusion Service for the changes to effect.

After restarting, the code will work and we can able to write the file in a Shared directory of some other computer.