/
file.php
105 lines (91 loc) · 2.64 KB
/
file.php
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
<?php
class DingesFile extends DingesInputField {
protected $minFileSize;
protected $maxFileSize;
function _setForm($form) {
parent::_setForm($form);
$form->setAttribute('enctype', 'multipart/form-data');
}
function setMinFileSize($bytes) {
$this->minFileSize = $bytes;
}
function setMaxFileSize($bytes) {
$this->maxFileSize = $bytes;
}
function setDefaultValue($value) {
throw new DingesException('DingesFile->setDefaultValue() has no use');
}
function getDefaultValue() {
throw new DingesException('DingesFile->getDefaultValue() has no use');
}
function validate($value) {
// Let op: DingesLabelField::validate(); niet parent::validate()
if(($error = DingesLabelField::validate($value)) !== true) {
return $error;
}
if($this->required && (!$value || $value['error'] == UPLOAD_ERR_NO_FILE)) {
return 'ERR_EMPTY';
}
if(!$value || $value['tmp_name'] == '') {
return true;
}
if($value['error'] == UPLOAD_ERR_PARTIAL || $value['error'] == UPLOAD_ERR_NO_TMP_DIR || $value['error'] == UPLOAD_ERR_CANT_WRITE || $value['error'] == UPLOAD_ERR_EXTENSION) {
return 'ERR_FILE_TECHNICAL';
}
if($value['error'] == UPLOAD_ERR_INI_SIZE || $value['error'] == UPLOAD_ERR_FORM_SIZE || ($this->maxFileSize !== NULL && $value['size'] > $this->maxFileSize)) {
return 'ERR_FILE_TOO_BIG';
}
if($this->minFileSize !== NULL && $value['size'] < $this->minFileSize) {
return 'ERR_FILE_TOO_SMALL';
}
if(!is_uploaded_file($value['tmp_name'])) {
// Hijack??
return 'ERR_FILE_TECHNICAL';
}
return true;
}
function getFileName() {
if($this->value) {
return $this->value['name'];
}
}
function getFileType() {
if($this->value) {
return $this->value['type'];
}
}
function getFileLocation() {
if($this->value) {
return $this->value['tmp_name'];
}
}
function getFileSize() {
if($this->value) {
return $this->value['size'];
}
}
function getFileContent() {
if($this->value) {
return file_get_contents($this->value['tmp_name']);
}
}
function moveFile($dest) {
if($this->value) {
return move_uploaded_file($this->value['tmp_name'], $dest);
} else {
throw new DingesException('You can not move this file due to the fact it is not uploaded');
}
}
function generateHTML() {
$out = parent::generateHTML();
if($this->maxFileSize !== NULL) {
return DingesForm::generateTag('input', array('type'=>'hidden', 'name'=>'MAX_FILE_SIZE', 'value'=>$this->maxFileSize)) . $out;
}
return $out;
}
function fillAttributes() {
parent::fillAttributes();
$this->setAttribute('type', 'file');
}
}
?>