Share Pastebin
Guest
Public paste!

Antonie Potgieter

By: a guest | Nov 20th, 2007 | Syntax: PHP | Size: 1.61 KB | Hits: 128 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. <?php
  2.  
  3. //start a PHP session.
  4.  
  5. class Upload {
  6.  
  7.         //upload directory.
  8.         //make sure it exists
  9.         //chmod it to 0777 to be writable
  10.         var $upload_dir = "uploads/";
  11.  
  12.         //the name of the file.
  13.         var $name = '';
  14.        
  15.         //TMP name of the file on server.
  16.         var $tmp_name = '';
  17.        
  18.         //the size of the file.
  19.         //this is measured in bytes
  20.         var $size = '';
  21.        
  22.         //the mime type of the file.
  23.         //ex. image/png
  24.         var $type = '';
  25.        
  26.         //number of files uploaded so far.
  27.         var $count = 1;
  28.  
  29.         function Upload($file = array()) {
  30.                 //make sure that the file data is not empty.
  31.                 if (!empty($file)) {
  32.                         //loop the file array
  33.                         foreach ($file as $key => $val) {
  34.                                 $this -> {$key} = $val;
  35.                         }
  36.                        
  37.                         //execute the uploading method.
  38.                         $this -> upload_file();
  39.                 }
  40.         }
  41.        
  42.         function upload_file() {
  43.                 //make sure the the file has been uploaded
  44.                 if (is_uploaded_file($this -> tmp_name)) {
  45.                         //make sure that the file can be moved to the "upload_dir"
  46.                         if (move_uploaded_file($this -> tmp_name, $this -> upload_dir . $this -> name)) {
  47.                                 //increment the file count.
  48.                                 $newcount = (!isset($_SESSION['filecount'])) ? $this -> count : ($_SESSION['filecount'] + 1);
  49.                                 $_SESSION['files'][$newcount]['filename'] = $this -> name;
  50.                                 $_SESSION['files'][$newcount]['filesize'] = $this -> size;
  51.                                 $_SESSION['files'][$newcount]['filetype'] = $this -> type;
  52.                         } else {
  53.                                 //file could not be moved
  54.                                 return false;
  55.                         }
  56.                 } else {
  57.                         //file was not uploaded
  58.                         return false;
  59.                 }
  60.         }
  61. }
  62.  
  63. //get all the posted file data
  64. $file = $_FILES['Filedata'];
  65.  
  66. //initialize the Upload class.
  67. $upload = new Upload($file);
  68.  
  69. ?>