Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 619
  • Last Modified:

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

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
soporific
Asked:
soporific
  • 9
  • 8
1 Solution
 
gbajramoCommented:
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
 
soporificAuthor Commented:
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
 
gbajramoCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
soporificAuthor Commented:
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
 
gbajramoCommented:
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
 
soporificAuthor Commented:
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
 
soporificAuthor Commented:
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
 
gbajramoCommented:
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
 
gbajramoCommented:
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
 
soporificAuthor Commented:
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
 
gbajramoCommented:
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
 
soporificAuthor Commented:
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
 
soporificAuthor Commented:
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
 
gbajramoCommented:
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
 
soporificAuthor Commented:
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
 
gbajramoCommented:
Wasn't aware of that. Thanks for letting me know.
0
 
soporificAuthor Commented:
Ditto.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now