Skip navigation
Currently Being Moderated

Can't delete uploaded file.

Jan 13, 2012 2:43 PM

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

 
Replies
  • Dave Watts
    747 posts
    Mar 11, 2003
    Currently Being Moderated
    Jan 13, 2012 5:26 PM   in reply to K0rrupt

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 14, 2012 1:29 AM   in reply to K0rrupt

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 14, 2012 1:54 PM   in reply to K0rrupt

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 15, 2012 4:57 AM   in reply to K0rrupt

     

    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

     
    |
    Mark as:
  • Dave Watts
    747 posts
    Mar 11, 2003
    Currently Being Moderated
    Jan 15, 2012 9:07 PM   in reply to K0rrupt

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 15, 2012 11:12 PM   in reply to K0rrupt

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 15, 2012 11:41 PM   in reply to K0rrupt

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 18, 2012 8:25 AM   in reply to K0rrupt

    Check into the <cfimage...> tag and releated image functions.  They can be used to provide all kinds of functionality to accomplish this task.  Such as telling you the actual diminsions of the image file as well as abilities to scale the image file if desired.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 18, 2012 12:48 PM   in reply to K0rrupt

    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>

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points