Whatever message this page gives is out now! Go check it out!
<cffile
action = "write"
file = "full pathname"
output = "content"
addNewLine = "yes|no"
attributes = "file attributes list"
charset = "character set option"
fixnewline = "yes|no"
mode = "permission">Attribute | Req/Opt | Default | Description |
action | Required | Type of file manipulation that the tag performs. | |
file | Required | Pathname of the file to write. 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. | |
output | Required | Content of the file to be created. | |
addNewLine | Optional | yes |
|
attributes | Optional | Applies to Windows. A comma-delimited list of attributes to set on the file. If omitted, the file's attributes are maintained.Each value must be specified explicitly. For example, if you specify attributes = "readOnly", all other attributes are overwritten.
| |
charset | Optional | JVM default file character set | The character encoding in which the file contents is encoded. The following list includes commonly used values:
|
fixnewline | Optional | no |
|
mode | Optional | Applies only to UNIX and Linux. Permissions. Octal values of UNIX chmod command. Assigned to owner, group, and other, respectively; for example:
|
<cffile action = "write"
file = "c:\files\updates\#Form.UpdateTitle#.txt"
output = "Created By: #Form.FullName#
Date: #Form.Date#
#Form.Content#">UpdateTitle = "FieldWork"
FullName = "World B. Frueh"
Date = "10/30/01"
Content = "We had a wonderful time in Cambridgeport."Created By: World B. Frueh
Date: 10/30/01
We had a wonderful time in Cambridgeport.<cffile action = "write"
file = "/tmp/foo"
mode = 644><cffile action = "append"
destination = "/home/tomj/testing.txt"
mode = 666
output = "Is this a test?">cffile action = "upload"
fileField = "fieldname"
destination = "/tmp/program.exe"
mode = 777><cfxml variable="xmlData">
<docroot>
<payload type="string">This is some plain text</payload>
</docroot>
</cfxml>
<cfset xmlString = toString(xmlData)>
<cfset key = createUUID()>
<cfset encString=encrypt(xmlString, key)>
<cffile action="write" addnewline="yes"
file="C:\ColdFusion9\wwwroot\test\store.dat"
output="#encString#" fixnewline="yes">
<cffile action="read" file="C:\ColdFusion9\wwwroot\test\store.dat"
variable="retrievedString">
<cfset decString=decrypt(retrievedString, key)>
<cfdump var="#decString#">
<cfset newXML = xmlParse(decString)>
<cfdump var="#newXML#"><!--- Create a new cfimage. --->
<cfset myImage=ImageNew("",200,200)>
<!--- Draw a square on the image. --->
<cfset ImageDrawRect(myImage,10,10,100,100)>
<!--- Use cffile to write the cfimage to a JPG. --->
<cffile action="write" output="#myImage#" file="c:\cfpix\square.jpg"><cfquery name="qDailySales" datasource="retailDB">
SELECT
transaction_date,
product_id,
product_name,
quantity,
unit_price,
total_amount,
tax_amount
FROM sales_transactions
WHERE CAST(transaction_date AS DATE) = CAST(GETDATE() AS DATE)
ORDER BY transaction_date
</cfquery>
<cfset reportContent = "Date,Product ID,Product Name,Quantity,Unit Price,Total,Tax#Chr(13)#Chr(10)#">
<cfloop query="qDailySales">
<cfset reportContent &= "#DateFormat(transaction_date, 'yyyy-mm-dd')#,">
<cfset reportContent &= "#product_id#,">
<cfset reportContent &= '"#product_name#",'>
<cfset reportContent &= "#quantity#,">
<cfset reportContent &= "#NumberFormat(unit_price, '9.99')#,">
<cfset reportContent &= "#NumberFormat(total_amount, '9.99')#,">
<cfset reportContent &= "#NumberFormat(tax_amount, '9.99')##Chr(13)#Chr(10)#">
</cfloop>
<cfset fileName = "sales_report_#DateFormat(Now(), 'yyyymmdd')#.csv">
<cfset filePath = "\\network\shared\accounting\daily_reports\#fileName#">
<cffile action="write"
file="#filePath#"
output="#reportContent#"
charset="utf-8"
mode="644">
<cflog text="Daily sales report generated: #fileName#" type="information"><cffunction name="logApplicationError" access="public" returntype="void">
<cfargument name="errorStruct" type="struct" required="true">
<cfargument name="userContext" type="struct" required="false" default="#StructNew()#">
<cfset var logDirectory = ExpandPath("/logs/application_errors")>
<cfset var logFileName = "error_log_#DateFormat(Now(), 'yyyy-mm-dd')#.log">
<cfset var logFilePath = "#logDirectory#/#logFileName#">
<!--- Ensure directory exists --->
<cfif NOT DirectoryExists(logDirectory)>
<cfdirectory action="create" directory="#logDirectory#">
</cfif>
<!--- Build detailed error log entry --->
<cfset var logEntry = "">
<cfset logEntry &= "========================================#Chr(13)#Chr(10)#">
<cfset logEntry &= "Timestamp: #DateFormat(Now(), 'yyyy-mm-dd')# #TimeFormat(Now(), 'HH:mm:ss')##Chr(13)#Chr(10)#">
<cfset logEntry &= "Error Type: #arguments.errorStruct.type#" & Chr(13) & Chr(10)>
<cfset logEntry &= "Error Message: #arguments.errorStruct.message#" & Chr(13) & Chr(10)>
<cfset logEntry &= "Error Detail: #arguments.errorStruct.detail#" & Chr(13) & Chr(10)>
<cfif StructKeyExists(arguments.userContext, "userID")>
<cfset logEntry &= "User ID: #arguments.userContext.userID#" & Chr(13) & Chr(10)>
</cfif>
<cfif StructKeyExists(arguments.userContext, "sessionID")>
<cfset logEntry &= "Session ID: #arguments.userContext.sessionID#" & Chr(13) & Chr(10)>
</cfif>
<cfset logEntry &= "Request URL: #CGI.SCRIPT_NAME#?#CGI.QUERY_STRING#" & Chr(13) & Chr(10)>
<cfset logEntry &= "Remote IP: #CGI.REMOTE_ADDR#" & Chr(13) & Chr(10)>
<cfset logEntry &= "User Agent: #CGI.HTTP_USER_AGENT#" & Chr(13) & Chr(10)>
<cfset logEntry &= "========================================" & Chr(13) & Chr(10) & Chr(13) & Chr(10)>
<!--- Append to log file --->
<cffile action="write"
file="#logFilePath#"
output="#logEntry#"
addnewline="no"
charset="utf-8"
mode="644">
</cffunction><cffunction name="updateTenantConfiguration" access="public" returntype="boolean">
<cfargument name="tenantID" type="numeric" required="true">
<cfargument name="configSettings" type="struct" required="true">
<cfset var configDir = ExpandPath("/config/tenants")>
<cfset var configFile = "#configDir#/tenant_#arguments.tenantID#_config.json">
<cfset var backupFile = "#configDir#/tenant_#arguments.tenantID#_config_backup_#DateFormat(Now(),'yyyymmdd')#_#TimeFormat(Now(),'HHmmss')#.json">
<cftry>
<!--- Backup existing configuration --->
<cfif FileExists(configFile)>
<cffile action="copy"
source="#configFile#"
destination="#backupFile#">
</cfif>
<!--- Prepare configuration structure --->
<cfset var configOutput = {
"tenantID" = arguments.tenantID,
"lastUpdated" = DateFormat(Now(), "yyyy-mm-dd") & " " & TimeFormat(Now(), "HH:mm:ss"),
"apiEndpoint" = arguments.configSettings.apiEndpoint,
"apiKey" = arguments.configSettings.apiKey,
"features" = {
"advancedReporting" = arguments.configSettings.advancedReporting,
"customBranding" = arguments.configSettings.customBranding,
"ssoEnabled" = arguments.configSettings.ssoEnabled
},
"branding" = {
"companyName" = arguments.configSettings.companyName,
"logoURL" = arguments.configSettings.logoURL,
"primaryColor" = arguments.configSettings.primaryColor
},
"limits" = {
"maxUsers" = arguments.configSettings.maxUsers,
"maxStorage" = arguments.configSettings.maxStorage
}
}>
<!--- Convert to JSON and write to file --->
<cfset var jsonConfig = SerializeJSON(configOutput)>
<cffile action="write"
file="#configFile#"
output="#jsonConfig#"
charset="utf-8"
mode="644">
<!--- Log the configuration update --->
<cflog text="Configuration updated for tenant #arguments.tenantID#"
type="information"
file="tenant_config">
<cfreturn true>
<cfcatch type="any">
<cflog text="Failed to update configuration for tenant #arguments.tenantID#: #cfcatch.message#"
type="error"
file="tenant_config">
<cfreturn false>
</cfcatch>
</cftry>
</cffunction><cffunction name="generateGDPRDataExport" access="public" returntype="string">
<cfargument name="customerID" type="numeric" required="true">
<cfset var exportDir = ExpandPath("/secure_exports/gdpr")>
<cfset var exportFileName = "customer_data_export_#arguments.customerID#_#CreateUUID()#.txt">
<cfset var exportFilePath = "#exportDir#/#exportFileName#">
<!--- Ensure directory exists --->
<cfif NOT DirectoryExists(exportDir)>
<cfdirectory action="create" directory="#exportDir#">
</cfif>
<!--- Retrieve customer data from database --->
<cfquery name="qCustomer" datasource="crmDB">
SELECT * FROM customers WHERE customer_id = <cfqueryparam value="#arguments.customerID#" cfsqltype="cf_sql_integer">
</cfquery>
<cfquery name="qOrders" datasource="crmDB">
SELECT * FROM orders WHERE customer_id = <cfqueryparam value="#arguments.customerID#" cfsqltype="cf_sql_integer">
</cfquery>
<cfquery name="qCommunications" datasource="crmDB">
SELECT * FROM communications WHERE customer_id = <cfqueryparam value="#arguments.customerID#" cfsqltype="cf_sql_integer">
</cfquery>
<!--- Build export content --->
<cfset var exportContent = "">
<cfset exportContent &= "GDPR DATA EXPORT REPORT" & Chr(13) & Chr(10)>
<cfset exportContent &= "Generated: #DateFormat(Now(), 'yyyy-mm-dd')# #TimeFormat(Now(), 'HH:mm:ss')#" & Chr(13) & Chr(10)>
<cfset exportContent &= "Customer ID: #arguments.customerID#" & Chr(13) & Chr(10)>
<cfset exportContent &= String(80, "=") & Chr(13) & Chr(10) & Chr(13) & Chr(10)>
<!--- Personal Information --->
<cfset exportContent &= "PERSONAL INFORMATION" & Chr(13) & Chr(10)>
<cfset exportContent &= String(40, "-") & Chr(13) & Chr(10)>
<cfloop query="qCustomer">
<cfset exportContent &= "Name: #first_name# #last_name#" & Chr(13) & Chr(10)>
<cfset exportContent &= "Email: #email#" & Chr(13) & Chr(10)>
<cfset exportContent &= "Phone: #phone#" & Chr(13) & Chr(10)>
<cfset exportContent &= "Address: #address#, #city#, #state# #zip#" & Chr(13) & Chr(10)>
<cfset exportContent &= "Account Created: #DateFormat(created_date, 'yyyy-mm-dd')#" & Chr(13) & Chr(10)>
</cfloop>
<cfset exportContent &= Chr(13) & Chr(10)>
<!--- Order History --->
<cfset exportContent &= "ORDER HISTORY" & Chr(13) & Chr(10)>
<cfset exportContent &= String(40, "-") & Chr(13) & Chr(10)>
<cfloop query="qOrders">
<cfset exportContent &= "Order ##: #order_id#" & Chr(13) & Chr(10)>
<cfset exportContent &= "Date: #DateFormat(order_date, 'yyyy-mm-dd')#" & Chr(13) & Chr(10)>
<cfset exportContent &= "Amount: $#NumberFormat(total_amount, '9.99')#" & Chr(13) & Chr(10)>
<cfset exportContent &= "Status: #status#" & Chr(13) & Chr(10) & Chr(13) & Chr(10)>
</cfloop>
<!--- Communication History --->
<cfset exportContent &= "COMMUNICATION HISTORY" & Chr(13) & Chr(10)>
<cfset exportContent &= String(40, "-") & Chr(13) & Chr(10)>
<cfloop query="qCommunications">
<cfset exportContent &= "Date: #DateFormat(comm_date, 'yyyy-mm-dd')#" & Chr(13) & Chr(10)>
<cfset exportContent &= "Type: #comm_type#" & Chr(13) & Chr(10)>
<cfset exportContent &= "Subject: #subject#" & Chr(13) & Chr(10)>
<cfset exportContent &= "Content: #content#" & Chr(13) & Chr(10) & Chr(13) & Chr(10)>
</cfloop>
<!--- Write to file --->
<cffile action="write"
file="#exportFilePath#"
output="#exportContent#"
charset="utf-8"
mode="600">
<!--- Log the export request --->
<cflog text="GDPR data export generated for customer #arguments.customerID#"
type="information"
file="gdpr_exports">
<cfreturn exportFileName>
</cffunction>