Guest
Public paste!

Antonie Potgieter

By: a guest | Nov 20th, 2007 | Syntax: PHP | Size: 2.60 KB | Hits: 353 | 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. ?>
  70.  
  71. <?php
  72.  
  73. //custom variable passed to the flash application
  74. $customVariable = $_GET['customVariable'];
  75. //the number of files uploaded
  76. $totalFileCount = $_GET['totalFileCount'];
  77.  
  78. //loop the uploaded files.
  79. //this is received from the flash application
  80. for ($i = 0; $i < $totalFileCount; $i++) {
  81.         $files[$i] = array(
  82.                 'filename' => $_GET['filename' . $i],
  83.                 'filesize' => $_GET['filesize' . $i],
  84.                 'filetype' => $_GET['filetype' . $i],
  85.         );     
  86. }
  87.  
  88. //appand some text to our data.
  89. $data = "Custom Variable - " . $customVariable . "\n";
  90. $data .= "Total File Count - " . $totalFileCount . "\n";
  91.  
  92. foreach ($files as $fKey => $fVal) {
  93.         foreach ($files[$fKey] as $fKey2 => $fVal2) {
  94.                 $data .= $fKey2 . " no " . $fKey . " - " . $fVal2 . "\n";
  95.         }      
  96. }
  97.  
  98. //open a new log file.
  99. $fh = fopen("uploads/test_" . time() . ".txt", "w");
  100. //write the generated data to the file.
  101. fwrite($fh, $data);
  102. //close the log file handle
  103. fclose($fh);
  104.  
  105. //response for the flash application
  106. echo 'success';
  107.  
  108. ?>