I made a simple upload form and trying to use the cffile to upload, rename and delete the original file. So the process goes like this.
Step 1: User will browse their computer to grab the picture they would like to upload (ex: picture1.jpg). then hit upload button.
Step 2: Use one cffile that would upload the file. (picture1.jpg)
Step 3: Another cffile would rename the original picture to the userID that i have in the database. (renamed to "1.jpg")
Step 4: Then another cffile to delete the orginial picture file, so there's only 1 picture which is the one the cffile renamed. (1.jpg on server, picture1.jpg deleted)
hope that makes sense. here's the code i used to try and make this happen.
I always get an error on the deleting part of the code.
=========== Upload form ===============
<cfform enctype="multipart/form-data" method="post">
<h1>Picture Upload Form</h1>
<cfinput type="file" name="fileUpload" required="yes" message="- Must add file!" />
<cfinput type="hidden" name="UserID" value="#URL.User#">
<cfinput type="submit" name="file_submit" value="Upload File" /><br />
</cfform>
<cfif isDefined("fileUpload")>
<cffile action="upload"
fileField="fileUpload"
destination="C:\ColdFusion9\wwwroot\website\pimages"
accept="image/jpg,image/jpeg,image/pjpeg"
nameconflict="makeunique">
<font size="+1">The file has been uploaded.</font>
<cffile action="rename"
source="#FORM.FileUpload#"
destination="C:\ColdFusion9\wwwroot\website\pimages\#FORM.UserID#.jpg ">
<cffile action="delete"
file="C:\ColdFusion9\wwwroot\website\pimages\#FORM.FileUpload#">
=========== Upload Form ===========
If I understand you, you have this:
* file x uploads to temp dir by the web server
* file x moved to file y by <cffile action="upload">
* file y renamed to file z by <cffile action="rename">.
* you then try to delete file y.
At this point file y already doesn't exist, because you've RENAMED IT TO file z. A rename operation does not result in two files, it results in one file: the original file name no longer exists; it is now the new file file name.
So, by the way you describe the operations you are performing (if I have it right), you don't need to do the delete anyhow.
?
Also you say you "can't delete the uploaded file" and you say you get an error, but you'd say what the error actually is...
--
Adam
What happens in the end is that i have a renamed file (from the original "picture1.jpg") to 1.jpg, so now i have 2 files. The one file the user uploaded and the one the cffile renamed. I would like to delete the picture1.jpg. I'll try and figure out another way to get rid of it like Dave said.
Thanks to both of you for the replies.
K0rrupt wrote:
What happens in the end is that i have a renamed file (from the original "picture1.jpg") to 1.jpg, so now i have 2 files. The one file the user uploaded and the one the cffile renamed. I would like to delete the picture1.jpg. I'll try and figure out another way to get rid of it like Dave said.
Thanks to both of you for the replies.
Do you understand that a RENAME process starts with one file, and ends up with one file (so a total of one files). One does not end up with the "first" file and the "result" file: the first file becomes the result file. So if one renames a file, one does not need to tidy up the original file, because it will no longer exist.
You still haven't posted the error message. This is the most important part of asking a question about failed code behaviour: what the error message is.
--
Adam
Here's the error:
File C:\ColdFusion9\wwwroot\pet_website\pimages\C:\ColdFusion9\runtime\ser vers\coldfusion\SERVER-INF\temp\wwwroot-tmp\neotmp1225387943055644545. tmp specified in action delete does not exist.
The error occurred in C:\ColdFusion9\wwwroot\pet_website\logged\pet\picture_upload2.cfm: line 42
40 :
41 : <cffile action="delete"
42 : file="C:\ColdFusion9\wwwroot\website\pimages\#FORM.FileUpload#">
43 :
44 : </cfif>
The thing with the rename function is that it makes a copy of the original file and renames the copy, So now i end up with 1.jpg and picture1.jpg. That's 1 file to many.
File C:\ColdFusion9\wwwroot\pet_website\pimages\C:\ColdFusion9\runtime\ser vers\coldfusion\SERVER-INF\temp\wwwroot-tmp\neotmp1225387943055644545 . tmp specified in action delete does not exist.
42 : file="C:\ColdFusion9\wwwroot\website\pimages\#FORM.FileUpload#">
Right. So actually look at the error message. You should be able to work out what you're doing wrong.
The thing with the rename function is that it makes a copy of the original file and renames the copy,
No, it doesn't.
--
Adam
Ok so i changed the upload location to the following.
<cfform enctype="multipart/form-data" method="post">
<h1>Picture Upload Form</h1>
<cfinput type="file" name="fileUpload" required="yes" message="- Must add file!" />
<cfinput type="hidden" name="UserID" value="#URL.User#">
<cfinput type="submit" name="file_submit" value="Upload File" /><br />
</cfform>
<cfif isDefined("fileUpload")>
<cffile action="upload"
fileField="fileUpload"
destination="C:\ColdFusion9\wwwroot\website\temp_images\"
accept="image/jpg,image/jpeg,image/pjpeg"
nameconflict="makeunique">
<font size="+1">The file has been uploaded.</font><br />
Don't forget to add the image name to the "Image" field.<br />
You can close this window now.
<cffile action="rename"
source="#FORM.FileUpload#"
destination="C:\ColdFusion9\wwwroot\website\pimages\#FORM.UserID#.jpg ">
</cfif>
So now my question is this. Is there a way i can delete "everything" in the C:\ColdFusion9\wwwroot\website\temp_images\ if i was to make another cfm file for this function?
Why don't you just upload the file where you want it in the first place?
Also, why not use ACTION="MOVE" instead of ACTION="RENAME"?
Now that I've asked those questions, yes, you could certainly have a file that used CFDIRECTORY to build a list of files in the directory, then used CFFILE to delete each in turn. Alternatively, you could build a file that ran a console command (like "del *") using CFEXECUTE.
Dave Watts, CTO, Fig Leaf Software
K0rrupt wrote:
<cffile action="delete"
file="C:\ColdFusion9\wwwroot\website\pimages\#FORM.FileUpload#">
FORM.FileUpload is the cause of the error. Better is:
<cffile action="delete"
file="C:\ColdFusion9\wwwroot\website\pimages\#cffile.serverFile#">
File C:\ColdFusion9\wwwroot\pet_website\pimages\C:\ColdFusion9\runtime\servers\coldfusion\SERVER-INF\temp\wwwroot-tmp\ neotmp1225387943055644545 . tmp specified in action delete does not exist.
By default, ColdFusion uploads files to the system temporary directory. The value of that directory is getTempDirectory(). Hence, by default, the value of FORM.FILE_FIELD_NAME is #getTempDirectory()#/#filename#. If no file name is specified, ColdFusion gives the file a temporary name, usually ending with the extension tmp. That explains the error message.
K0rrupt wrote:
So now my question is this. Is there a way i can delete "everything" in the C:\ColdFusion9\wwwroot\website\temp_images\ if i was to make another cfm file for this function?
Make sure there are some files in C:\ColdFusion9\wwwroot\website\temp_image. Then do the following test beforehand.
<cfdirectory action="list" name="myDir" directory=" C:\ColdFusion9\wwwroot\website\temp_images" recurse="false">
<cfdump var="#myDir#">
This lets you see how ColdFusion stores the directory as a query object. The code you require follows.
<cfdirectory action="list" name="myDir" directory=" C:\ColdFusion9\wwwroot\website\temp_images" recurse="false">
<cfloop query="myDir">
<cfif myDir.type is "file">
<cffile action="delete" file="#myDir.directory#\#myDir.name#" >
</cfif>
</cfloop>
done deleting files in directory
One more quick question...well more like advice. I got to thinking with people uploading pictures and not all of them will be the same size. The picture frame i made on the site for the image is roughly 250px by 250px. Now is there a way that i can make the picture frame size up to the image even if it's smaller then the frame box i set? I know web browsers blur and somewhat distort images if they are resized, so that why i have been struggling with this problem.
Any advice will be great. Just need a point in the right direction.
Create a top/bottom section of frame that is 1-10 pxls wide
and right/left that is 1-10 pxls high
<table cellpadding="0" cellspacing="0">
<tr>
<td><img src="left top corner of frame"></td>
<td background="top of frame.png"></td><!--- will repeat along width of client's image --->
<td><img src="right top corner of frame"></td>
</tr>
<tr>
<td background="right of frame.png"></td><!--- will repeat along height of client's image --->
<td bgcolor="silver"><cfimage action="info" srcfile="#expandpath(thesrc)#">
<cfif cfimage.height gt cfimage.width>
<cfif cfimage.height gt "250">
<img src="client's image" height="250">
<cfelse>
<img src="client's image">
</cfif>
<cfelse>
<cfif cfimage.width gt "250">
<img src="client's image" width="250">
<cfelse>
<img src="client's image">
</cfif>
</cfif>
</td>
<td background="right of frame.png"></td><!--- will repeat along height of client's image --->
</tr>
<tr>
<td><img src="left bottom corner of frame"></td>
<td background="bottom of frame.png"></td><!--- will repeat along width of client's image --->
<td><img src="right bottom corner of frame"></td>
</tr>
North America
Europe, Middle East and Africa
Asia Pacific