Choosing Upload Method

php Uploader

PHP File Uploader

Choosing a method to save the uploaded files

There are three ways to handle file uploading with PHP 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. <?php require_once "phpuploader/include_phpuploader.php" ?>   
  2. <html>   
  3. <body>   
  4.        <?php   
  5.             $uploader=new PhpUploader();   
  6.             $uploader->Name="myuploader";   
  7.             $uploader->SaveDirectory="/myfolder";   
  8.             $uploader->Render();   
  9.         ?>                  
  10. </body>   
  11. </html>  

That's all that's needed. Really!

Standard PHP Upload

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

  1. <?php require_once "phpuploader/include_phpuploader.php" ?>   
  2. <html>   
  3. <body>   
  4.         <form id="form1" method="POST">   
  5.             <?php   
  6.                 $uploader=new PhpUploader();   
  7.                 $uploader->Name="myuploader";    
  8.                 $uploader->Render();   
  9.             ?>   
  10.         </form>   
  11. <?php   
  12. //Gets the GUID of the file based on uploader name   
  13. $fileguid=@$_POST["myuploader"];   
  14. if($fileguid)   
  15. {   
  16.     //get the uploaded file based on GUID   
  17.     $mvcfile=$uploader->GetUploadedFile($fileguid);   
  18.     if($mvcfile)   
  19.     {   
  20.         //Gets the name of the file.   
  21.         echo($mvcfile->FileName);   
  22.         //Gets the temp file path.   
  23.         echo($mvcfile->FilePath);   
  24.         //Gets the size of the file.   
  25.         echo($mvcfile->FileSize);    
  26.            
  27.         //Copys the uploaded file to a new location.   
  28.         $mvcfile->CopyTo("/uploads");   
  29.         //Moves the uploaded file to a new location.   
  30.         $mvcfile->MoveTo("/uploads");   
  31.         //Deletes this instance.   
  32.         $mvcfile->Delete();   
  33.     }   
  34. }   
  35. ?>  
  36. </body>   
  37. </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. <?php require_once "phpuploader/include_phpuploader.php" ?>   
  2. <html>   
  3. <body>   
  4.        <?php   
  5.             $uploader=new PhpUploader();   
  6.             $uploader->Name="myuploader";   
  7.             //Create a new file upload handler   
  8.             $uploader->UploadUrl="myhandler.php";   
  9.             $uploader->Render();   
  10.         ?>                  
  11. </body>   
  12. </html>  

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

 

  1. <?php require_once "phpuploader/include_phpuploader.php" ?>   
  2. <?php   
  3.     $uploader=new PhpUploader();   
  4.   
  5.     $mvcfile=$uploader->GetValidatingFile();   
  6.   
  7.     if($mvcfile->FileName=="accord.bmp")   
  8.     {   
  9.      $uploader->WriteValidationError("My custom error : Invalid file name. ");   
  10.          exit(200);   
  11.     }   
  12.   
  13.     //USER CODE:   
  14.   
  15.     $targetfilepath"savefiles/myprefix_" . $mvcfile->FileName;   
  16.     ifis_file ($targetfilepath) )   
  17.      unlink($targetfilepath);   
  18.     $mvcfile->MoveTo( $targetfilepath );   
  19.   
  20.     $uploader->WriteValidationOK();   
  21.   
  22. ?>