Manuel Pear

HTTP_Upload

Easy and secure managment of files submitted via HTML forms.

Introduction

This package provides an advanced system for managing uploads of files via HTML <input type="file" /> fields. Features include:

Examples

In the following examples it is assumed that you are using an HTML form field <input type="file" name="f" /> in order to upload files. For example:

Exemple 48-30. HTML form for simple file upload

The following form can be used in order to test the single file upload example.

<?php
// sample code from below goes here
?>
<html>
 <head>
 </head>
 <body>
  <form name="fileuploadexample" method="POST" enctype="multipart/form-data"
   action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>">
   <input type="file" name="f" />
   <input type="submit" name="submit" value="Submit" />
  </form>
 </body>
</html>

Exemple 48-31. Simple file upload

The following code looks at the request and checks if a valid file was uploaded through the form. If that is the case, the file is moved to the subdirectory uploads.

  1. <?php
  2. require_once "HTTP/Upload.php";  
  3.  
  4. $upload = new HTTP_Upload("en");  
  5. $file = $upload->getFiles("f");  
  6.  
  7. if ($file->isValid()) { 
  8.    $moved = $file->moveTo("uploads/"); 
  9.    if (!PEAR::isError($moved)) { 
  10.       echo "File was moved to uploads/"; 
  11.    } else { 
  12.       echo $moved->getMessage(); 
  13.    }  
  14. } elseif ($file->isMissing()) { 
  15.    echo "No file was provided.";  
  16. } elseif ($file->isError()) { 
  17.    echo $file->errorMsg();  
  18. ?> 

Exemple 48-32. HTML form for multiple file upload

The following form can be used in order to test the multiple file upload example.

<?php
// sample code from below goes here
?>
<html>
 <head>
 </head>
 <body>
  <form name="fileuploadexample2" method="POST" enctype="multipart/form-data"
   action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>">
   <input type="file" name="f1" />
   <input type="file" name="f2" />
   <input type="submit" name="submit" value="Submit" />
  </form>
 </body>
</html>

Exemple 48-33. Multiple files, more extensive checks

Multiple files can uploaded by replacing the name of the form field (f) with f[] and creating multiple <input /> fields with this name.

  1. <?php
  2. $upload = new HTTP_Upload("en");  
  3. $files = $upload->getFiles();  
  4.  
  5. foreach($files as $file){ 
  6.    if (PEAR::isError($file)) { 
  7.       echo $file->getMessage(); 
  8.    } 
  9.  
  10.    if ($file->isValid()) { 
  11.       $file->setName("uniq"); 
  12.       $dest_name = $file->moveTo("uploads/"); 
  13.  
  14.       if (PEAR::isError($dest_name)) { 
  15.          echo $dest_name->getMessage(); 
  16.       } 
  17.  
  18.       $real = $file->getProp("real"); 
  19.  
  20.    } elseif ($file->isMissing()) { 
  21.       echo "No file was provided."; 
  22.    } elseif ($file->isError()) { 
  23.       echo $file->errorMsg(); 
  24.    } 
  25.  
  26.    print_r($file->getProp());  
  27. ?> 

Extensive information about uploaded files

HTTP_Upload provides extensive information about uploaded files via the getProp() method:

mixed HTTP_Upload_File::getProp ([name])

If no value for name is provided, then this method will return an array containing all available information about the uploaded file. Otherwise the information identified by the value of this parameter will be returned as a string.

The list of possible values is determined by the contents of the $_FILES array, but is customized for the purposes of HTTP_Upload. Here are the possible properties:

  • 'name': destination file name

  • 'tmp_name': temporary uploaded file name (assigned by PHP)

  • 'form_name': name of the HTML form that submitted the uploaded file

  • 'type': Mime type of the file

  • 'size': size of the file

  • 'error': if there was an error on upload, this contains a string representing the kind of error. The errorCode() method can be used to retrieve a localized error message from this property.

Exemple 48-34. Extensive information via getProp()

  1. <?php
  2. require_once "HTTP/Upload.php";  
  3.  
  4. $upload = new HTTP_Upload("en");  
  5. $file = $upload->getFiles("f");  
  6.  
  7. if ($file->isValid()) { 
  8.    echo "<pre>"; 
  9.    print_r($file->getProp()); 
  10.    echo "</pre>"; 
  11.  
  12.    printf("The uploaded file has the extension %s.", $file->getProp("ext"));  
  13. ?> 

Internationalized Error Messages

Another handy feature of HTTP_Upload is support for internationalized error messages. This means that if an error (like an invalid file upload) is detected, the programmer can choose in which the language the error messages should be returned by HTTP_Upload.

The first parameter of the constructor method for HTTP_Upload determines the language to be used. This is illustrated in the following example:

Exemple 48-35. Example

  1. <?php
  2. // German error messages
  3. $language = "de";  
  4.  
  5. require_once "HTTP/Upload.php";  
  6.  
  7. $upload = new HTTP_Upload($language);  
  8. $file = $upload->getFiles("f");  
  9.  
  10. if ($file->isValid()) { 
  11.    $moved = $file->moveTo("uploads/"); 
  12.    if (!PEAR::isError($moved)) { 
  13.       echo "File was moved to uploads/"; 
  14.    } else { 
  15.       // This will print a german error message
  16.       echo "An error was detected: " . $moved->getMessage() . "<br />"; 
  17.    }  
  18. ?> 

Remonter Remonter
L'éditeur javascript - CSS - Gentoo - Tutoriaux PHP - Tutoriels PHP - Bretagne - php - Moto - Kit graphique