summaryrefslogtreecommitdiffstats
path: root/includes/file_upload.php
blob: d62397fab516fd680ce9469c43785f5ab004b52b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/******************************
 * EQdkp
 * Copyright 2002-2003
 * Licensed under the GNU GPL.  See COPYING for full terms.
 * ------------------
 * file_upload.php
 * begin: Sat December 21 2002
 *
 * $Id: file_upload.php 46 2007-06-19 07:29:11Z tsigo $
 *
 ******************************/

if ( !defined('EQDKP_INC') )
{
     die('Do not access this file directly.');
}

class fileupload{

  private $upload_tmp_dir = "/tmp/";  // leading and trailing slash required
  private $file_upload_flag = "off";
  private $upload_max_filesize = "100";
  private $allowable_upload_base_dirs = array("/tmp/", "/var/www/localhost/");
  private $allowable_upload_tmp_dirs = array( "/tmp/");
  private $upload_dir= "/tmp/";  // leading and trailing slash required
  private $upload_file_name;

  function __construct($name) {
     if( is_null($_FILES[$name]) )  {
         echo "Specified file <strong> ".$name." </strong> does not exist in the FILES array. Please check if it exists";
         echo "Exiting...";
         exit;
     }
     $this->getConfigurationSettings();
     if( $this->file_upload_flag == "off" ) {
       echo "File upload capability in the configuration file is turned <strong> off </strong> . Please update the php.ini file.";
       exit;
     }
     $this->upload_file_name = $name;
  }

  private function getConfigurationSettings() {
     $this->file_upload_flag = ini_get('file_uploads');
     $this->upload_tmp_dir = ini_get('upload_tmp_dir');
     $this->upload_max_filesize = ini_get('upload_max_filesize');
     $this->upload_max_filesize = preg_replace('/M/', '000000', $this->upload_max_filesize);
  }

  public function getErrors() {
     return $_FILES[$this->upload_file_name]['error'];
  }

  public function getFileSize() {
     return $_FILES[$this->upload_file_name]['size'];
  }

  public function getFileName() {
     return $_FILES[$this->upload_file_name]['name'];
  }

  public function getTmpName() {
     return $_FILES[$this->upload_file_name]['tmp_name'];
  }

  public function setUploadDir($upload_dir) {
   trim($upload_dir);
   if( $upload_dir[strlen($upload_dir)-1] != "/" ) $upload_dir .= "/"; // add trailing slash
   $can_upload = false;
   foreach( $this->allowable_upload_base_dirs as $dir ) {
       if( $dir == $upload_dir ) {
     $can_upload = true;
         break;
       }
   }
   if( !$can_upload ) {
       echo "Cannot upload to the dir ->".$upload_dir;
       return;
   }else{
       $this->upload_dir = $upload_dir;
       echo $this->upload_dir;
   }
  }

  public function setTmpUploadDir($upload_tmp_dir) {
   trim($upload_tmp_dir);
   if( $upload_tmp_dir[strlen($upload_tmp_dir)-1] != "/" ) $upload_tmp_dir .= "/"; // add trailing slash
   $can_upload = false;
   foreach( $this->allowable_upload_base_dirs as $dir ) {
       if( $dir == $upload_tmp_dir ) {
     $can_upload = true;
     return;
       }
   }
   if( !$can_upload ) {
       echo "Cannot upload to the dir ->".$uplaod_tmp_dir;
       return;
   }
   $this->upload_tmp_dir = $upload_dir;
  }

  public function uploadFile() {
   if( $this->checkMaxMemorySizeLimit() ) {
       echo "File size of ".$this->getFileSize()." greater than allowable limit of ".$this->upload_max_filesize."Please change the configuration setting.";
       return;
   }else{
     if( !move_uploaded_file($this->getTmpName(), $this->upload_dir.$this->getFileName()) ) {
         echo "Failed to upload file ".$this->getTmpName();
     }
   }
  }

  public function checkMaxMemorySizeLimit() {
   if( $this->getFileSize() >  $this->upload_max_filesize ) {
     return true;
   }else{
     return false;
   }
  }

}

?>