PHP File Uploader

Choosing a method to save the uploaded files

There are three ways to handle file uploading with ASP Uploader. You can choose one based on your application requirements.


Simple Upload

Simple Upload is designed for the simplest usage scenario. It calls the SaveDirectory method of the control which actually performs the upload. It automatically parses the information POSTed by the browser, figures out how many files are being uploaded, and saves them in a specified local directory on the server under their original names. An attachment table is also created.

  1. <%Language="VBScript" %>  
  2. <!-- #include file="aspuploader/include_aspuploader.asp" -->  
  3. <html>   
  4. <body>   
  5.         <form id="form1" method="POST">   
  6.             <%
  7.                 Dim uploader   
  8.                 Set uploader=new AspUploader   
  9.                 uploader.Name="myuploader"
  10.                 uploader.SaveDirectory="/myfolder"
  11.                 uploader.Render()  
  12.             %>
  13.         </form>   
  14. </body>   
  15. </html>  

That's all that's needed. Really!

Standard ASP Upload

If your usage scenario is close to the simplest case, described above, but you need a little more control, you can use Standard ASP Upload. It allows the ASP developers to process the uploaded files using ASP code. The page posts back to itself to process the uploaded files.

  1. <%@ Language="VBScript" %>   
  2. <!-- #include file="aspuploader/include_aspuploader.asp" -->  
  3. <html>   
  4. <head>   
  5. </head>   
  6. <body>   
  7.         <form id="form1" method="POST">   
  8.         <%   
  9.             Dim uploader   
  10.             Set uploader=new AspUploader   
  11.             uploader.Name="myuploader"  
  12.             uploader.Render()   
  13.         %>   
  14.         </form>   
  15.                
  16. <%   
  17.   
  18. If Request.Form("myuploader")&""<>"" Then  
  19.     Dim mvcfile   
  20.     Set mvcfile=uploader.GetUploadedFile(Request.Form("myuploader"))   
  21.        
  22.     Response.Write("<div style='font-family:Fixedsys'>")   
  23.     Response.Write("Uploaded File")   
  24.     Response.Write("<br/><br/>FileName: ")   
  25.     Response.Write(mvcfile.FileName)   
  26.     Response.Write("<br/>FileSize: ")   
  27.     Response.Write(mvcfile.FileSize)   
  28. '   Response.Write("<br/>FilePath: ")   
  29. '   Response.Write(mvcfile.FilePath)   
  30.     Response.Write("</div>")   
  31. End If  
  32.   
  33. %>   
  34.                            
  35. </body>   
  36. </html>  

Custom file upload handler

If you need further control over the parsing of the file uploading request, you can write a custom file upload handler. In handler page (UploadUrl) developers can rename the uploaded files, process other logics programmatically.

  1. <%@ Language="VBScript" %>   
  2. <!-- #include file="aspuploader/include_aspuploader.asp" -->  
  3. <html>   
  4. <head>   
  5. </head>   
  6. <body>   
  7.     <%   
  8.         Dim uploader   
  9.         Set uploader=new AspUploader   
  10.         uploader.UploadUrl="myhandler.asp"  
  11.         uploader.Render()   
  12.     %>   
  13. </body>   
  14. </html>  

The following code shows you how to rename the uploaded files, process other logics programmatically in handler page (UploadUrl).

  1. <%@ Language="VBScript" %>   
  2. <!-- #include file="aspuploader/include_aspuploader.asp" -->  
  3.   
  4. <%   
  5.     Dim uploader, mvcfile   
  6.     Set uploader=new AspUploader   
  7.     Set mvcfile=uploader.GetValidatingFile()   
  8.     If mvcfile.FileName = "some.bmp" then   
  9.         uploader.WriteValidationError("My custom error : Invalid file name. ")   
  10.         Response.End  
  11.     End if   
  12.        
  13.     targetfilepath= "savefiles/myprefix_" & mvcfile.FileName   
  14.        
  15.     dim fs   
  16.     Set fs=Server.CreateObject("Scripting.FileSystemObject")   
  17.     if fs.FileExists(server.mappath(targetfilepath)) then   
  18.       fs.DeleteFile(server.mappath(targetfilepath))   
  19.     end if   
  20.     set fs=nothing   
  21.        
  22.     mvcfile.MoveTo( server.mappath(targetfilepath))   
  23.   
  24.     uploader.WriteValidationOK()   
  25. %>