Solved

George Petrov Pure ASP Upload - Old File Not Deleted When Updating Record

Posted on 2004-10-29
552 Views
Last Modified: 2006-11-17
I've come across a problem where I'm unable to delete an image from my uploaded images folder when updating.  The database record is being written just fine and the new image is put in place without a problem.  I'm using George Petrov's PureASP Upload 2.09 extension for the upload script.  

PureUpload - Using Full Path
Site on a virtual directory (read, write and scripts allowed)
Recordset is opened at the very top of the page
The images folder is 3 levels deep from root (i.e. root/admin/inventory/images)

Here is the delete code; the code in asterisks is the only code I've changed from the Dreamweaver auto generated and PureUpload generated:

****************
<%
Function newFileSystemObject()
set newFileSystemObject=Server.CreateObject("Scripting.FileSystemObject")
End Function
%>

<%
Function fileExists(aFileSpec)
fileExists=newFileSystemObject.fileExists(aFileSpec)
End Function
%>

****************

<%

' Update Record: (Modified for File Upload) construct a sql update statement and execute it

If (CStr(UploadFormRequest("MM_update")) <> "" And CStr(UploadFormRequest("MM_recordId")) <> "") Then

  ' create the sql update statement
  MM_editQuery = "update " & MM_editTable & " set "
  For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
    MM_formVal = MM_fields(MM_i+1)
    MM_typeArray = Split(MM_columns(MM_i+1),",")
    MM_delim = MM_typeArray(0)
    If (MM_delim = "none") Then MM_delim = ""
    MM_altVal = MM_typeArray(1)
    If (MM_altVal = "none") Then MM_altVal = ""
    MM_emptyVal = MM_typeArray(2)
    If (MM_emptyVal = "none") Then MM_emptyVal = ""
    If (MM_formVal = "") Then
      MM_formVal = MM_emptyVal
    Else
      If (MM_altVal <> "") Then
        MM_formVal = MM_altVal
      ElseIf (MM_delim = "'") Then  ' escape quotes
        MM_formVal = "'" & Replace(MM_formVal,"'","''") & "'"
      Else
        MM_formVal = MM_delim + MM_formVal + MM_delim
      End If
    End If
    If (MM_i <> LBound(MM_fields)) Then
      MM_editQuery = MM_editQuery & ","
    End If
    MM_editQuery = MM_editQuery & MM_columns(MM_i) & " = " & MM_formVal
  Next
  MM_editQuery = MM_editQuery & " where " & MM_editColumn & " = " & MM_recordId

  If (Not MM_abortEdit) Then
    ' execute the update
    Set MM_editCmd = Server.CreateObject("ADODB.Command")
    MM_editCmd.ActiveConnection = MM_editConnection

****************      

            ' This is where we delete the file before we delete the record!
            Set File = CreateObject("Scripting.FileSystemObject")
            ImagePath = Server.MapPath("\")
            ImagePath = ImagePath & "\" & (Recordset1.Fields.Item("Photo").Value)
            
            ' check if file exists and if true delete the file
            If fileExists(ImagePath) Then
            File.DeleteFile(ImagePath)
            End If

***************

    MM_editCmd.CommandText = MM_editQuery
    MM_editCmd.Execute
    MM_editCmd.ActiveConnection.Close

    If (MM_editRedirectUrl <> "") Then
      Response.Redirect(MM_editRedirectUrl)
    End If
  End If

End If
%>

I hope this makes some sense.  I've used this script without a problem before, but it's not working on my home IIS even with full rights given to the IUSR account.  Please let me know if you would like more information.

Thanks
0
Question by:soporific
    17 Comments
     
    LVL 2

    Expert Comment

    by:gbajramo
    It sounds like permissions issue. Just for testing, in Windows Explorer, right click on the directory, go to security tab, and add Everyone. Then give full control to Everyone and try again.
    0
     

    Author Comment

    by:soporific
    I set the images folder to Everyone (Full Control) and then also did the root folder, still no deletions, and no errors either.  Is there some way I can view a log to see what it's processing?

    Thanks for the reply,

    ~S
    0
     
    LVL 2

    Expert Comment

    by:gbajramo
    Try outputing your path to make sure it is mapped correctly. Insert

    Response.write(ImagePath)

    before checking if file exists. It's very likely that delete statment is never executed because the file is not found by FileExists.
    0
     

    Author Comment

    by:soporific
    Sorry, I haven't used Response.write in a page yet.  I'm not sure where to find that output; it's not showing up on the page.  Also I'm getting ID=1& at the of the URL when it forwards to the succeed page, which I don't remember ever happening to any of my other pages before (if that makes a difference).

    ~S
    0
     
    LVL 2

    Expert Comment

    by:gbajramo
    Here is your excerpt along with Response.write. Just drop it in your code and you will see output when you navigate to your page:

    ****************    

              ' This is where we delete the file before we delete the record!
              Set File = CreateObject("Scripting.FileSystemObject")
              ImagePath = Server.MapPath("\")
              ImagePath = ImagePath & "\" & (Recordset1.Fields.Item("Photo").Value)
             
    Response.write(ImagePath)
    Response.end

              ' check if file exists and if true delete the file
              If fileExists(ImagePath) Then
              File.DeleteFile(ImagePath)
              End If

    ***************
    0
     

    Author Comment

    by:soporific
    Ok, I'll try it in just a couple minutes.  I didn't have reponse end, but that's exactly where I put Reponse.Write.

    Thanks,

    ~S
    0
     

    Author Comment

    by:soporific
    Now I see what's happening:

    c:\inetpub\wwwroot\/Lodging/admin/inventory/images/country_inn_tiny.g

    I have my virtual directory on my D drive, basically I told IIS to make a virtual directory named Lodging on the D drive and it seems to me that it's trying to edit off C instead of D.  Is this the case, or should it be okay anyways?
    0
     
    LVL 2

    Accepted Solution

    by:
    I'm gathering the following from your message:
    1. Your scripts are sitting on the C drive
    2. Your images are sitting on a virtual directory on D drive

    This is what's happening

    Server.MapPath("\") ===> c:\inetpub\wwwroot

    Then you append path from recordset which results in

    "c:\inetpub\wwwroot\/Lodging/admin/inventory/images/country_inn_tiny.g"

    If your images are on D drive, this obviously won't work because:

    If fileExists("c:\inetpub\wwwroot\/Lodging/admin/inventory/images/country_inn_tiny.g") ===> is false

    So you have two choices.
    1. You can make sure that your images are in the above path by moving them from D to C
    2. You can run your script with Server.MapPath from your virtual directory on drive D which would give you correct path to the images on D drive.

    0
     
    LVL 2

    Expert Comment

    by:gbajramo
    Sorry, missed an option:

    3. You can do Server.MapPath("virtualDirectoryName") which will give you path to the virtual directory root. So you would do following in your code:

    Replace

    Server.MapPath("\")

    with

    Server.MapPath("virtualDirectoryName")

    Try that first.
    0
     

    Author Comment

    by:soporific
    Actually all of my code is in the D drive along with the images.  I just pointed the virtual IIS site to the D drive because my C drive space is sparse.  The only thing on C is the "Default Web Site".  Also, when you say Server.MapPath("virtualDirectoryName") would that be the actual path, or is it a location used by the script?

    I'm giving you the points because I think you have gotten me on the right track.  Thanks for your patience.  If you have anything further to add, I would appreciate it.

    One more question actually, why would it be able to upload the file correctly, but then not find it to delete it?

    Thanks again,

    ~S
    0
     
    LVL 2

    Expert Comment

    by:gbajramo
    You are using different objects to upload and delete file. I am not familiar with PureAsp but FSO object requires phisical path to find and delete the file.

    Server.MapPath("insert your own virtual directory name here") ===> will result in the physical path to the virtual directory you created on D drive. Try it in your code and then do response.write to ensure you are getting the correct path on D drive.
    0
     

    Author Comment

    by:soporific
    Okay, I solved my problem and here's what I had to do:

    Moved my physical directory over to C:\inetpub\wwwroot\%Directory%
    Changed the actual %Directory% name to Lodging to match the virtual directory name

    Also, is it possible to ask direct questions to users over Experts Exchange?  I really like the way you answer questions and would rather give people like you the points.

    ~S

    0
     

    Author Comment

    by:soporific
    Just saw your last reply, I will give the Server.MapPath("virtualPath") solution a try just to learn a little bit more about it.  Thanks for giving me some more insight into how the FSO object works and the fact that they are separate.  This really beats pulling my hair out and you've been very helpful in getting me to understand more.

    ~S
    0
     
    LVL 2

    Expert Comment

    by:gbajramo
    You are welcome. Thanks for the points.

    I'm not sure how would you go about posting questions directly to somebody. I think it would be a nice feature and would allow building of smaller support groups. I guess you could let me know when you need something and then add to EE once it's resolved for points. If I'm not available, you can post it to EE right away.
    0
     

    Author Comment

    by:soporific
    Stupid question, but how would I reach you?  I actually have another question I'd like answered about books.  i.e., which ones might be helpful to someone like me.  I'll give it 100 points if that's okay as I just want a basic opinion.
    0
     
    LVL 2

    Expert Comment

    by:gbajramo
    Wasn't aware of that. Thanks for letting me know.
    0
     

    Author Comment

    by:soporific
    Ditto.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Accessibility and Usability are two concepts that seem to be closely related.  But, too many people seem to have a distorted perception of them. During last five years, those two words have come to the day-to-day work of almost every web develope…
    Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
    This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

    931 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now