The nameconflict attribute doesn't seem well documented, so maybe I'm using it wrong. But if I write a file (binary data) using the following code, it's overwriting the existing file of the same name.
<cffile action="write" file="/path/#data.name#" output="#d#" nameconflict="MakeUnique" mode="775">
nameconflict="MakeUnique" only works for <cffile action="upload".
Overwriting the file is default behavior for <cffile action="write"
Unless exact file names are not needed for display purpose it is better approach to use a unique ID as the filename to make sure we do not overwrite files.
<cfset newFile = '/path/' & CreateUUID() & '.' & ListLast(data.name, '.')>
<cffile action="write" file="#newFile#" output="#d#" mode="775">
Note: For use cases where we need to have a custom file name for view purposes, its better to save that original file name in the database and keep the filename in the filesystem in separate column.
I am having a hard time figuring out how to use cfschedule and hope someone can help get me unstuck.
I can schedule a task fine. The Admin portal indicates that the task has run, but I don't see that anything has happened.
<cfschedule
action="update"
task="update_printer_status"
operation="HTTPRequest"
startDate="03/26/2018"
startTime="12:00 PM"
url="http://localhost:8000/source/update_status.cfm"
interval="1800" />
For testing purposes, I am just trying to get it to save the date/time it runs into a log file. But the log file is empty. Here's my log file.
<!--- Read in existing contents of file --->
<cffile
action="read"
file = "c:\temp\update_log.txt"
variable = "file_contents"
>
<!--- Set additional content --->
<cfset br = chr(13) & chr(10) />
<cfset date="#DateFormat(Now())#, #TimeFormat(Now())#">
<!--- Write string back to log file --->
<cfset update_string="#file_contents##br#Update initiated: #date#">
<cfscript>
FileWrite("update_string", "#update_string#");
ustr = FileRead("update_string");
FileWrite("c:\temp\update_log.txt", "#ustr#");
</cfscript>
Can anyone tell me what I'm doing wrong here?
My issue is that I had saved my module in a protected directly. Since the context is not logged, the administrator was not seeing it.
To resolve, I just moved my file to a public directory.
I'm having an issue with a coldfusion11 website, under certain conditions my attempt to create and send a report file is failing, apparently due to permission issues. I can't recreate the issue on in my test/dev environment so I need to understand what is happening for a live fix, I can't just start arbitrarily changing code on production. The current code creates the file using an APPEND action like this:
<cfset f_dir = EXCEL_PATH >
<cfset f_name = CreateUUID() & ".csv">
<cffile action="APPEND" file="#f_dir##f_name#" output="My Report "
addnewline="Yes">
<cffile action="APPEND" file="#f_dir##f_name#"
output="Title,#attributes.title#" addnewline="Yes">
Elsewhere in the code are similar functions that use WRITE as the first cffile action, like this:
<cfset f_dir = EXCEL_PATH >
<cfset f_name = CreateUUID() & ".csv">
<cffile action="WRITE" file="#f_dir##f_name#" output="My Report "
addnewline="Yes">
<cffile action="APPEND" file="#f_dir##f_name#"
output="Title,#attributes.title#" addnewline="Yes">
The code that uses WRITE first is not failing, this leads to my question:
Is there a difference between creating a file with action="APPEND" and action="WRITE" in coldfusion 11?
Ron - Write is "create" - it makes a new file. Append is used to add data to an existing file on the disk. They are different actions.
Try modifying your code as follows:
<cflock name="#f_name#">
<cfif NOT fileexists(f_dir & f_name)>
... do your WRITE action>
</cfif>
</cflock>
<cflock name="#f_name#">
.... do your APPEND action - you can be sure your file exists at this point.
</cflock>
Note, I usually use a named lock to serialize these two actions. Sometimes your code trips over file handles not quite released. Not typical but if your disk is ever thrashing it can happen.
I am trying to fix some code for a client. The issue is, we have .CSV files that are being written in ColdFusion but duplicates are constantly being generated for some reason, even with nameConflict = "Overwrite" in there. I am seeing that nameConflict may not even work with cffile action="write", how could I add maybe just an ascending number to the end of a duplicate file name every time it is generated? The CSV files simply show customer renewal information.
<cfif arguments.isRenewal EQ "Y">
<cffile action="write" file="#filename#" nameconflict="overwrite" output='"Capital","#custName#","","#getCustomer.fname#","#getCustomer.lname#","#getCustomer.serviceAddress1#","#getCustomer.serviceAddress2#","#getCustomer.serviceCity#","#getCustomer.serviceState#","#getCustomer.serviceZip#","#cleanPhone#","#cleanMobilePhone#","#getCustomer.email#","#getCustomer.billingAddress1#","#getCustomer.billingAddress2#","#getCustomer.billingCity#","#getCustomer.billingState#","#getCustomer.billingZip#","0","N/A","Electric","#getCustomer.sdi#","#getCustomer.sdi#","#getProvider.utilityCode#","#getOffer.rateSchedule#","#getOffer.productCode#","#getOffer.rate/100#","kWh","$","#getOffer.rackRateName#","#DateFormat(now(), "yyyy-mm-dd")#","#startDate#","#getOffer.term#","Internet","","","","","","","","","","","","","M2M","#getOffer.evergreenProduct#","Y"'>
<cfelse>
<cffile action="write" file="#filename#" nameconflict="overwrite" output='"Capital","#custName#","","#getCustomer.fname#","#getCustomer.lname#","#getCustomer.serviceAddress1#","#getCustomer.serviceAddress2#","#getCustomer.serviceCity#","#getCustomer.serviceState#","#getCustomer.serviceZip#","#cleanPhone#","#cleanMobilePhone#","#getCustomer.email#","#getCustomer.billingAddress1#","#getCustomer.billingAddress2#","#getCustomer.billingCity#","#getCustomer.billingState#","#getCustomer.billingZip#","0","N/A","Electric","#getCustomer.sdi#","#getCustomer.sdi#","#getProvider.utilityCode#","#getOffer.rateSchedule#","#getOffer.productCode#","#getOffer.rate/100#","kWh","$","#getOffer.rackRateName#","#DateFormat(now(), "yyyy-mm-dd")#","#startDate#","#getOffer.term#","Internet","","","","","","","","","","","","","M2M","#getOffer.evergreenProduct#","N"'>
duplicates snapshot
I'm able to get all the values that I want from cfldap.
But when I try to update the user image I don't know how to send the correct value for the binary image attribute.
I tried getting the image variable from the cffile upload
<cffile action="UPLOAD" filefield="file" destination="c:\inetpub\wwwroot\test" nameconflict="OVERWRITE" result="image" />
Also tried using cfimage with a static image -
<cfimage action="read" source="c:\inetpub\wwwroot\test\image.png" name="anotherImage">
Or even with
<cffile action="READBINARY" file="c:\inetpub\wwwroot\test\image.png" variable="BinaryImageContent">
But in any case, when I call
<cfldap action="modify"
DN="#results.dn#"
attributes="thumbnailPhoto=#obj.image#"
modifytype="replace"
server="myserver"
username="mydomain\myuser"
password="mypass">
The #results.dn# is the DN from the user that I get before (Everything ok on that)
I created the #obj.image# to be able to try all types of variables
Also tried these params:
<cfset obj.test1 = BinaryImageContent />
<cfdump var="#imageGetBlob(anotherImage)#" />
<cfdump var="#toString(obj.test1)#" />
By the way, the error that I get its
One or more of the required attributes may be missing or incorrect or
you do not have permissions to execute this operation on the server.
The problem is that I'm using the domain administrator account to update that
(THIS ERROR IS SOLVED - The network guys hadn't given me this permission... now I have it).
Now what I'm using is the following:
<cffile action="UPLOAD" filefield="file" destination="c:\inetpub\wwwroot\test" nameconflict="OVERWRITE" result="imagem" />
<cfset filename = "C:\inetpub\wwwroot\test\#imagem.serverFile#">
<cffile action="readbinary" file="#filename#" variable="img">
<cfset imgStr = BinaryEncode(img, "hex")>
<cfset imgStr2 = REReplace(imgStr, "..", "\\\0", "ALL")>
<cfldap
action="modify"
DN="#results.dn#"
attributes="thumbnailPhoto=#imgStr2#"
modifytype="replace"
server="myserver"
username="mydomain\myuser"
password="mypass"
>
but I get this binary code
Whats strange, is that before I had a binary code like -1-41 and now, nothing similar...
and when I try to show the pic
And this is one correct image....
EDIT: The original code sample below shows how it could work if ColdFusion wouldn't have a bug (or "very unfortunate design decision") in CFLDAP.
CFLDAP encodes the parameter values you pass to it before sending them to the server. This is nice because you don't have to worry about value encoding. But... it is also not helpful because it means you can't send encoded values yourself anymore, since CF invariably encodes them again.
Bottom line: As far as LDAP is concerned, encoding a file into a hex-string is correct, but CFLDAP mangles that string before sending it to the server. Combined with the fact that CFLDAP does not accept raw binary data this means that you can't use it to update binary attributes.
The comments contain a suggestion for a 3rd-party command line tool that can easily substitute CFLDAP for this task.
You need to send an encoded string to the server as the attribute value. The encoding scheme for binary data in LDAP queries has the form of attribute=\01\02\03\ab\af\cd.
Read your image into a byte array, encode that array into a hex string and prefix every encoded byte with a backslash.
<cffile action="readbinary" file="#filename#" variable="img">
<cfset imgStr = BinaryEncode(img, "hex")>
<cfset imgStr = REReplace(imgStr, "..", "\\\0", "ALL")>
<cfldap
action="modify"
DN="#results.dn#"
attributes="thumbnailPhoto=#imgStr#"
modifytype="replace"
server="myserver"
username="mydomain\myuser"
password="mypass"
>
Also don't forget what the documentation has to say about modifyType.