I don't know if i'm a great coder or not.
I don't even know if i'm doing things the "right way"... but i thought it might be interesting for shits and giggles to share some code for a change.
I write almost everything in CFML because i really enjoy the simplicity of coldFusion vs. ASP or PHP. on my servers and dev environment, i'm still running ColdFusion 4.5 (i'm thinking of upgrading 5.0 or maybe even MX, but only for more XML interactivity, but the cost... ouch.)
So given all of that, i thought i'd release some of the code for how my wecam's save image works on the backend.
The scope was this: I have a webcam that every n-seconds ftp's an image to my server called webcam.jpg. I wanted the ability, to click on a button and save the current image. ok no problem.
but i also didn't want to just fill my disk with images. There should be a maximum number of saved images, and everytime you save one, it factors it down and expires the oldest one. simple enough, right? so i built this in a couple of minutes with cf
This assumes that your server has enabled CFFILE, which many hosted services do not.
<!--- set vars --->
<cfset dirPath=#GetDirectoryFromPath(ExpandPath("./"))#>
<cfset imgCount=8>
<!--- delete the oldest image --->
<cfset oldName=#dirPath# & "webcam" & imgCount & ".jpg">
<cfoutput>DELETING file #oldName#<br /></cfoutput>
<cftry>
<cffile action=“DELETE” file=“#oldName#”>
<b>succsess!</b><br />
<cfcatch>
Error.<br />
</cfcatch>
</cftry>
<!--- now loop through copying the new images down the line --->
<cfloop from=“#eval(imgCount-1)#” to=“1” index=“i” step=“-1”>
<cfset oldName=#dirPath# & “webcam” & i & “.jpg”>
<cfset newName=#dirPath# & “webcam” & #evaluate(i+1)# & “.jpg”>
<cfoutput>file #oldName# renamed to #newName#<br /></cfoutput>
<cftry>
<cffile action=“RENAME” source=“#oldName#” destination=“#newName#”>
<b>succsess!</b><br />
<cfcatch>
<b>error.</b><br />
</cfcatch>
</cftry>
</cfloop>
<!--- now copy over the new one --->
<cfset oldName=#dirPath# & “webcam.jpg”>
<cfset newName=#dirPath# & “webcam1.jpg”>
<cfoutput>new img: file #oldName# copied to #newName#<br /></cfoutput>
<cftry>
<cffile action=“COPY” source=“#oldName#” destination=“#newName#”>
<b>succsess!</b><br />
<cfcatch>
<b>error.</b><br />
</cfcatch>
</cftry>
<!--- back to original page --->
<cflocation url=“#CGI.HTTP_REFERER#”>