Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezhttpfile.php
Go to the documentation of this file.
1 <?php
22 class eZHTTPFile
23 {
24 const UPLOADEDFILE_OK = 0;
25 const UPLOADEDFILE_DOES_NOT_EXIST = -1;
26 const UPLOADEDFILE_EXCEEDS_PHP_LIMIT = -2;
27 const UPLOADEDFILE_EXCEEDS_MAX_SIZE = -3;
28 const UPLOADEDFILE_MISSING_TMP_DIR = -4;
29 const UPLOADEDFILE_CANT_WRITE = -5;
30 const UPLOADEDFILE_UNKNOWN_ERROR = -6;
31
35 public function __construct( $http_name, $variable )
37 {
38 $this->HTTPName = $http_name;
39 $this->OriginalFilename = $variable["name"];
40 $this->Type = $variable["type"];
41 $mime = explode( "/", $this->Type );
42 $this->MimeCategory = $mime[0];
43 $this->MimePart = $mime[1];
44 $this->Filename = $variable["tmp_name"];
45 $this->Size = $variable["size"];
46 $this->IsTemporary = true;
47 }
48
52 function storageDir( $sub_dir = false )
53 {
54 $sys = eZSys::instance();
55 $storage_dir = $sys->storageDirectory();
56 if ( $sub_dir !== false )
57 $dir = $storage_dir . "/$sub_dir/" . $this->MimeCategory;
58 else
59 $dir = $storage_dir . "/" . $this->MimeCategory;
60 return $dir;
61 }
62
66 function store( $sub_dir = false, $suffix = false, $mimeData = false )
67 {
68 if ( !$this->IsTemporary )
69 {
70 eZDebug::writeError( "Cannot store non temporary file: " . $this->Filename,
71 "eZHTTPFile" );
72 return false;
73 }
74 $this->IsTemporary = false;
75
76 $ini = eZINI::instance();
77 // $storage_dir = $ini->variable( "FileSettings", "VarDir" ) . '/' . $ini->variable( "FileSettings", "StorageDir" );
78 $storage_dir = eZSys::storageDirectory();
79 if ( $sub_dir !== false )
80 $dir = $storage_dir . "/$sub_dir/";
81 else
82 $dir = $storage_dir . "/";
83 if ( $mimeData )
84 $dir = $mimeData['dirpath'];
85
86 if ( !$mimeData )
87 {
88 $dir .= $this->MimeCategory;
89 }
90
91 if ( !file_exists( $dir ) )
92 {
93 if ( !eZDir::mkdir( $dir, false, true ) )
94 {
95 return false;
96 }
97 }
98
99 $suffixString = false;
100 if ( $suffix != false )
101 $suffixString = ".$suffix";
102
103 if ( $mimeData )
104 {
105 $dest_name = $mimeData['url'];
106 }
107 else
108 {
109 $dest_name = $dir . "/" . md5( basename( $this->Filename ) . microtime() . mt_rand() ) . $suffixString;
110 }
111
112 if ( !move_uploaded_file( $this->Filename, $dest_name ) )
113 {
114 eZDebug::writeError( "Failed moving uploaded file " . $this->Filename . " to destination $dest_name" );
115 unlink( $dest_name );
116 $ret = false;
117 }
118 else
119 {
120 $ret = true;
121 $this->Filename = $dest_name;
122 $perm = $ini->variable( "FileSettings", "StorageFilePermissions" );
123 $oldumask = umask( 0 );
124 chmod( $dest_name, octdec( $perm ) );
125 umask( $oldumask );
126
127 // Write log message to storage.log
128 $storageDir = $dir . "/";
129 eZLog::writeStorageLog( basename( $this->Filename ), $storageDir );
130 }
131 return $ret;
132 }
133
137 function attributes()
138 {
139 return array( "original_filename",
140 "filename",
141 "filesize",
142 "is_temporary",
143 "mime_type",
144 "mime_type_category",
145 "mime_type_part" );
146 }
147
151 function hasAttribute( $attr )
152 {
153 return in_array( $attr, $this->attributes() );
154 }
155
159 function attribute( $attr )
160 {
161 switch ( $attr )
162 {
163 case "original_filename":
164 return $this->OriginalFilename;
165 case "mime_type":
166 return $this->Type;
167 case "mime_type_category":
168 return $this->MimeCategory;
169 case "mime_type_part":
170 return $this->MimePart;
171 case "filename":
172 return $this->Filename;
173 case "filesize":
174 return $this->Size;
175 case "is_temporary":
176 return $this->IsTemporary;
177 default:
178 {
179 eZDebug::writeError( "Attribute '$attr' does not exist", __METHOD__ );
180 return null;
181 } break;
182 }
183 }
184
203 static function canFetch( $httpName, $maxSize = false )
204 {
205 if ( !isset( $GLOBALS["eZHTTPFile-$httpName"] ) ||
206 !( $GLOBALS["eZHTTPFile-$httpName"] instanceof eZHTTPFile ) )
207 {
208 if ( $maxSize === false )
209 {
210 return isset( $_FILES[$httpName] ) and $_FILES[$httpName]['name'] != "" and $_FILES[$httpName]['error'] == 0;
211 }
212
213 if ( isset( $_FILES[$httpName] ) and $_FILES[$httpName]['name'] != "" )
214 {
215 switch ( $_FILES[$httpName]['error'] )
216 {
217 case ( UPLOAD_ERR_NO_FILE ):
218 {
219 return eZHTTPFile::UPLOADEDFILE_DOES_NOT_EXIST;
220 }break;
221
222 case ( UPLOAD_ERR_INI_SIZE ):
223 {
224 return eZHTTPFile::UPLOADEDFILE_EXCEEDS_PHP_LIMIT;
225 }break;
226
227 case ( UPLOAD_ERR_FORM_SIZE ):
228 {
229 return eZHTTPFile::UPLOADEDFILE_EXCEEDS_MAX_SIZE;
230 }break;
231
232 case ( UPLOAD_ERR_NO_TMP_DIR ):
233 {
234 return eZHTTPFile::UPLOADEDFILE_MISSING_TMP_DIR;
235 }break;
236
237 case ( UPLOAD_ERR_CANT_WRITE ):
238 {
239 return eZHTTPFile::UPLOADEDFILE_CANT_WRITE;
240 }break;
241
242 case ( 0 ):
243 {
244 return ( $maxSize == 0 || $_FILES[$httpName]['size'] <= $maxSize )? eZHTTPFile::UPLOADEDFILE_OK:
245 eZHTTPFile::UPLOADEDFILE_EXCEEDS_MAX_SIZE;
246 }break;
247
248 default:
249 {
250 return eZHTTPFile::UPLOADEDFILE_UNKNOWN_ERROR;
251 }
252 }
253 }
254 else
255 {
256 return eZHTTPFile::UPLOADEDFILE_DOES_NOT_EXIST;
257 }
258 }
259 if ( $maxSize === false )
260 return eZHTTPFile::UPLOADEDFILE_OK;
261 else
262 return true;
263 }
264
269 static function fetch( $http_name )
270 {
271 if ( !isset( $GLOBALS["eZHTTPFile-$http_name"] ) ||
272 !( $GLOBALS["eZHTTPFile-$http_name"] instanceof eZHTTPFile ) )
273 {
274 $GLOBALS["eZHTTPFile-$http_name"] = null;
275
276 if ( isset( $_FILES[$http_name] ) and
277 $_FILES[$http_name]["name"] != "" )
278 {
279 $mimeType = eZMimeType::findByURL( $_FILES[$http_name]['name'] );
280 $_FILES[$http_name]['type'] = $mimeType['name'];
281 $GLOBALS["eZHTTPFile-$http_name"] = new eZHTTPFile( $http_name, $_FILES[$http_name] );
282 }
283 else
284 {
285 eZDebug::writeError( "Unknown file for post variable: $http_name",
286 "eZHTTPFile" );
287 }
288 }
289 return $GLOBALS["eZHTTPFile-$http_name"];
290 }
291
295 function setMimeType( $mime )
296 {
297 $this->Type = $mime;
298 list ( $this->MimeCategory, $this->MimePart ) = explode( '/', $mime, 2 );
299 }
300
302 public $HTTPName;
304 public $OriginalFilename;
306 public $Type;
308 public $MimeCategory;
310 public $MimePart;
312 public $Filename;
314 public $Size;
316 public $IsTemporary;
317 }
318
319 ?>
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZDir\mkdir
static mkdir( $dir, $perm=false, $recursive=false)
Definition: ezdir.php:73
eZHTTPFile
Provides access to HTTP post files.
Definition: ezhttpfile.php:23
eZHTTPFile\UPLOADEDFILE_MISSING_TMP_DIR
const UPLOADEDFILE_MISSING_TMP_DIR
Definition: ezhttpfile.php:28
eZHTTPFile\$Filename
$Filename
The local filename.
Definition: ezhttpfile.php:312
eZHTTPFile\UPLOADEDFILE_EXCEEDS_MAX_SIZE
const UPLOADEDFILE_EXCEEDS_MAX_SIZE
Definition: ezhttpfile.php:27
eZHTTPFile\$MimeCategory
$MimeCategory
The mimetype category (first part)
Definition: ezhttpfile.php:308
eZHTTPFile\storageDir
storageDir( $sub_dir=false)
Definition: ezhttpfile.php:52
eZHTTPFile\setMimeType
setMimeType( $mime)
Definition: ezhttpfile.php:295
eZHTTPFile\canFetch
static canFetch( $httpName, $maxSize=false)
Definition: ezhttpfile.php:203
eZHTTPFile\$HTTPName
$HTTPName
The name of the HTTP file.
Definition: ezhttpfile.php:302
eZHTTPFile\attributes
attributes()
Definition: ezhttpfile.php:137
eZHTTPFile\fetch
static fetch( $http_name)
Definition: ezhttpfile.php:269
eZHTTPFile\attribute
attribute( $attr)
Definition: ezhttpfile.php:159
eZHTTPFile\UPLOADEDFILE_UNKNOWN_ERROR
const UPLOADEDFILE_UNKNOWN_ERROR
Definition: ezhttpfile.php:30
eZHTTPFile\$Type
$Type
The mime type of the file.
Definition: ezhttpfile.php:306
eZHTTPFile\UPLOADEDFILE_EXCEEDS_PHP_LIMIT
const UPLOADEDFILE_EXCEEDS_PHP_LIMIT
Definition: ezhttpfile.php:26
eZHTTPFile\$Size
$Size
The size of the local file.
Definition: ezhttpfile.php:314
eZHTTPFile\UPLOADEDFILE_CANT_WRITE
const UPLOADEDFILE_CANT_WRITE
Definition: ezhttpfile.php:29
eZHTTPFile\$MimePart
$MimePart
The mimetype type (second part)
Definition: ezhttpfile.php:310
eZHTTPFile\$OriginalFilename
$OriginalFilename
The original name of the file from the client.
Definition: ezhttpfile.php:304
eZHTTPFile\hasAttribute
hasAttribute( $attr)
Definition: ezhttpfile.php:151
eZHTTPFile\UPLOADEDFILE_OK
const UPLOADEDFILE_OK
Definition: ezhttpfile.php:24
eZHTTPFile\store
store( $sub_dir=false, $suffix=false, $mimeData=false)
Definition: ezhttpfile.php:66
eZHTTPFile\UPLOADEDFILE_DOES_NOT_EXIST
const UPLOADEDFILE_DOES_NOT_EXIST
Definition: ezhttpfile.php:25
eZHTTPFile\__construct
__construct($http_name, $variable)
Definition: ezhttpfile.php:35
eZHTTPFile\$IsTemporary
$IsTemporary
Whether the file is a temporary file or if it has been moved(stored).
Definition: ezhttpfile.php:316
eZLog\writeStorageLog
static writeStorageLog( $name, $dir=false)
Definition: ezlog.php:85
eZMimeType\findByURL
static findByURL( $url, $returnDefault=true)
Definition: ezmimetype.php:227
eZSys\storageDirectory
static storageDirectory()
Definition: ezsys.php:514
eZSys\instance
static instance()
Definition: ezsys.php:1272
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.