Advertisement
Guest User

Antonie Potgieter

a guest
Nov 20th, 2007
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.60 KB | None | 0 0
  1. <?php
  2.  
  3. //start a PHP session.
  4. session_start();
  5.  
  6. class Upload {
  7.  
  8.     //upload directory.
  9.     //make sure it exists
  10.     //chmod it to 0777 to be writable
  11.     var $upload_dir = "uploads/";
  12.  
  13.     //the name of the file.
  14.     var $name = '';
  15.    
  16.     //TMP name of the file on server.
  17.     var $tmp_name = '';
  18.    
  19.     //the size of the file.
  20.     //this is measured in bytes
  21.     var $size = '';
  22.    
  23.     //the mime type of the file.
  24.     //ex. image/png
  25.     var $type = '';
  26.    
  27.     //number of files uploaded so far.
  28.     var $count = 1;
  29.  
  30.     function Upload($file = array()) {
  31.         //make sure that the file data is not empty.
  32.         if (!empty($file)) {
  33.             //loop the file array
  34.             foreach ($file as $key => $val) {
  35.                 $this -> {$key} = $val;
  36.             }
  37.            
  38.             //execute the uploading method.
  39.             $this -> upload_file();
  40.         }
  41.     }
  42.    
  43.     function upload_file() {
  44.         //make sure the the file has been uploaded
  45.         if (is_uploaded_file($this -> tmp_name)) {
  46.             //make sure that the file can be moved to the "upload_dir"
  47.             if (move_uploaded_file($this -> tmp_name, $this -> upload_dir . $this -> name)) {
  48.                 //increment the file count.
  49.                 $newcount = (!isset($_SESSION['filecount'])) ? $this -> count : ($_SESSION['filecount'] + 1);
  50.                 $_SESSION['files'][$newcount]['filename'] = $this -> name;
  51.                 $_SESSION['files'][$newcount]['filesize'] = $this -> size;
  52.                 $_SESSION['files'][$newcount]['filetype'] = $this -> type;
  53.             } else {
  54.                 //file could not be moved
  55.                 return false;
  56.             }
  57.         } else {
  58.             //file was not uploaded
  59.             return false;
  60.         }
  61.     }
  62. }
  63.  
  64. //get all the posted file data
  65. $file = $_FILES['Filedata'];
  66.  
  67. //initialize the Upload class.
  68. $upload = new Upload($file);
  69.  
  70. ?>
  71.  
  72. <?php
  73.  
  74. //custom variable passed to the flash application
  75. $customVariable = $_GET['customVariable'];
  76. //the number of files uploaded
  77. $totalFileCount = $_GET['totalFileCount'];
  78.  
  79. //loop the uploaded files.
  80. //this is received from the flash application
  81. for ($i = 0; $i < $totalFileCount; $i++) {
  82.     $files[$i] = array(
  83.         'filename' => $_GET['filename' . $i],
  84.         'filesize' => $_GET['filesize' . $i],
  85.         'filetype' => $_GET['filetype' . $i],
  86.     ); 
  87. }
  88.  
  89. //appand some text to our data.
  90. $data = "Custom Variable - " . $customVariable . "\n";
  91. $data .= "Total File Count - " . $totalFileCount . "\n";
  92.  
  93. foreach ($files as $fKey => $fVal) {
  94.     foreach ($files[$fKey] as $fKey2 => $fVal2) {
  95.         $data .= $fKey2 . " no " . $fKey . " - " . $fVal2 . "\n";
  96.     }  
  97. }
  98.  
  99. //open a new log file.
  100. $fh = fopen("uploads/test_" . time() . ".txt", "w");
  101. //write the generated data to the file.
  102. fwrite($fh, $data);
  103. //close the log file handle
  104. fclose($fh);
  105.  
  106. //response for the flash application
  107. echo 'success';
  108.  
  109. ?>
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement