Skip to main content
January 13, 2012
Answered

Can't delete uploaded file.

  • January 13, 2012
  • 4 replies
  • 5127 views

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 ===========

    This topic has been closed for replies.
    Correct answer BKBK

    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?


    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

    4 replies

    Participating Frequently
    January 18, 2012

    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>

    BKBK
    Community Expert
    Community Expert
    January 16, 2012

    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.

    Inspiring
    January 14, 2012

    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

    January 14, 2012

    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.

    Inspiring
    January 14, 2012

    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

    Community Expert
    January 14, 2012

    You often can't delete a file if you create or modify it in the same CF program. The way I usually handle this sort of thing is to have a separate program that runs, often on a scheduled basis, to delete files (perhaps en mass).

    Dave Watts, CTO, Fig Leaf Software

    Dave Watts, Eidolon LLC