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.
- <?php require_once "phpuploader/include_phpuploader.php" ?>
- <html>
- <body>
- <?php
- $uploader=new PhpUploader();
- $uploader->Name="myuploader";
- $uploader->SaveDirectory="/myfolder";
- $uploader->Render();
- ?>
- </body>
- </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.
- <?php require_once "phpuploader/include_phpuploader.php" ?>
- <html>
- <body>
- <form id="form1" method="POST">
- <?php
- $uploader=new PhpUploader();
- $uploader->Name="myuploader";
- $uploader->Render();
- ?>
- </form>
- <?php
- //Gets the GUID of the file based on uploader name
- $fileguid=@$_POST["myuploader"];
- if($fileguid)
- {
- //get the uploaded file based on GUID
- $mvcfile=$uploader->GetUploadedFile($fileguid);
- if($mvcfile)
- {
- //Gets the name of the file.
- echo($mvcfile->FileName);
- //Gets the temp file path.
- echo($mvcfile->FilePath);
- //Gets the size of the file.
- echo($mvcfile->FileSize);
- //Copys the uploaded file to a new location.
- $mvcfile->CopyTo("/uploads");
- //Moves the uploaded file to a new location.
- $mvcfile->MoveTo("/uploads");
- //Deletes this instance.
- $mvcfile->Delete();
- }
- }
- ?>
- </body>
- </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.
- <?php require_once "phpuploader/include_phpuploader.php" ?>
- <html>
- <body>
- <?php
- $uploader=new PhpUploader();
- $uploader->Name="myuploader";
- //Create a new file upload handler
- $uploader->UploadUrl="myhandler.php";
- $uploader->Render();
- ?>
- </body>
- </html>
The following code shows you how to rename the uploaded files, process other logics programmatically in handler page (UploadUrl).
- <?php require_once "phpuploader/include_phpuploader.php" ?>
- <?php
- $uploader=new PhpUploader();
- $mvcfile=$uploader->GetValidatingFile();
- if($mvcfile->FileName=="accord.bmp")
- {
- $uploader->WriteValidationError("My custom error : Invalid file name. ");
- exit(200);
- }
- //USER CODE:
- $targetfilepath= "savefiles/myprefix_" . $mvcfile->FileName;
- if( is_file ($targetfilepath) )
- unlink($targetfilepath);
- $mvcfile->MoveTo( $targetfilepath );
- $uploader->WriteValidationOK();
- ?>