Foutmelding bij acculade
Code (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
124
125
126
127
<?php
/**
* @package uploader
*
* @author Niels Kieviet <[email protected]>
* @version $Revision v1.00$
* @copyright Copyright (c) 2010, Niels Kieviet
*/
class uploader
{
/**
* @var Array
*/
protected $extensions;
/**
* @var Array
*/
protected $types;
/**
* @var Integer
*/
protected $size;
/**
* @var Array
*/
protected $errors;
/**
* Contructor
*
* @param Array $extensions
* @param Array $types
* @param Integer $maxSize
*/
public function __construct( Array $extentions = array(), Array $types = array(), $size = 200000 )
{
$this->errors = new ErrorList();
$this->extentions = $extentions;
$this->types = $types;
$this->size = $size;
}
/**
* Upload the file
*
* @param String $file
* @param String $path
*/
public function upload( $file, $path )
{
if( $this->isAllowed( $file ) && $this->pathExists( $path ) ) {
// doe hier het uploaden.
}
}
/**
* Check if file is allow
*
* @param String $file
* @return Boolean
*/
protected function isAllowed( $file )
{
return ( $this->checkMime( $file ) && $this->checkExtension( $file ) && $this->checkSize( $file ) );
}
/**
* Check MimeType
*
* @param String $file
* @return Boolean
*/
protected function checkMime( $file )
{
if( count( $this->types ) > 0 ) {
if( !in_array( mime( $file ), $this->types ) ) {
$this->errors[] = 'This mime-type is not allowed.';
return false;
}
return true;
}
return true;
}
/**
* Check extension
*
* @param String $file
* @return Boolean
*/
protected function checkExtension( $file )
{
if( count ( $this->extensions ) > 0 ) {
if( !in_array( $file, $this->extensions ) ) {
$this->errors[] = 'This extension is not allowed';
return false;
}
return true;
}
return true;
}
/**
* Check size
*
* @param String $file
* @return Boolean
*/
protected function checkSize( $file )
{
if( count( $this->size ) > 0 ) {
// Control heigth and width
if( !getimagesize( $file['tmp_name'] ) ) {
$this->errors[] = 'The file doenst have a valid height or width';
return false;
}
// Controle file size
if( $file['size'] > $this->size ) {
$this->errors[] = 'The file is too big';
return false;
}
return true;
}
}
?>
/**
* @package uploader
*
* @author Niels Kieviet <[email protected]>
* @version $Revision v1.00$
* @copyright Copyright (c) 2010, Niels Kieviet
*/
class uploader
{
/**
* @var Array
*/
protected $extensions;
/**
* @var Array
*/
protected $types;
/**
* @var Integer
*/
protected $size;
/**
* @var Array
*/
protected $errors;
/**
* Contructor
*
* @param Array $extensions
* @param Array $types
* @param Integer $maxSize
*/
public function __construct( Array $extentions = array(), Array $types = array(), $size = 200000 )
{
$this->errors = new ErrorList();
$this->extentions = $extentions;
$this->types = $types;
$this->size = $size;
}
/**
* Upload the file
*
* @param String $file
* @param String $path
*/
public function upload( $file, $path )
{
if( $this->isAllowed( $file ) && $this->pathExists( $path ) ) {
// doe hier het uploaden.
}
}
/**
* Check if file is allow
*
* @param String $file
* @return Boolean
*/
protected function isAllowed( $file )
{
return ( $this->checkMime( $file ) && $this->checkExtension( $file ) && $this->checkSize( $file ) );
}
/**
* Check MimeType
*
* @param String $file
* @return Boolean
*/
protected function checkMime( $file )
{
if( count( $this->types ) > 0 ) {
if( !in_array( mime( $file ), $this->types ) ) {
$this->errors[] = 'This mime-type is not allowed.';
return false;
}
return true;
}
return true;
}
/**
* Check extension
*
* @param String $file
* @return Boolean
*/
protected function checkExtension( $file )
{
if( count ( $this->extensions ) > 0 ) {
if( !in_array( $file, $this->extensions ) ) {
$this->errors[] = 'This extension is not allowed';
return false;
}
return true;
}
return true;
}
/**
* Check size
*
* @param String $file
* @return Boolean
*/
protected function checkSize( $file )
{
if( count( $this->size ) > 0 ) {
// Control heigth and width
if( !getimagesize( $file['tmp_name'] ) ) {
$this->errors[] = 'The file doenst have a valid height or width';
return false;
}
// Controle file size
if( $file['size'] > $this->size ) {
$this->errors[] = 'The file is too big';
return false;
}
return true;
}
}
?>
Dit script geeft deze error
Parse error: syntax error, unexpected ';', expecting T_FUNCTION in ....
line: 127 = de laatste }
Ik snap niet waarom deze fout komt. Iemand een idee?
Ik denk dat je deze ook nog met afsluiten aan het eind van je script.
Gewijzigd op 16/06/2010 12:32:46 door Bas IJzelendoorn
Ooo schaam, wat een lompe fout, Ik heb daarvoor gewoon een half uur naar gezocht. Tijd voor pauze denk ik
Gewijzigd op 16/06/2010 12:33:56 door Bas IJzelendoorn
Ja het werkt nu:) Voor de rest nog commentaar of kritiek op mijn OOP denkwijze?
Je zou de extensies misschien in een andere class kunnen doen, waarin je de meest gebruikte extensies definieert. Iedere extensie/type heeft namelijk weer een andere validatie.. ;-)
Die snap ik even niet. Het is toch gewoon een in_array, en upload is toch gewoon move_uploaded_file /folder/filename/ext
?
Code (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
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
<?php
class Upload
{
public function addCheck(FileCheck $check)
{
$this->checks[] = $check;
}
public function check($file)
{
// checkcheckcheckcheckcheckcheck
foreach($this->checks as $check)
if(!$check->check($file))
return false;
return true;
}
}
interface FileCheck
{
public function check($path);
}
class FileMimeCheck implements FileCheck
{
private $mimeTypes;
public function __construct(array $allowedMimeTypes)
{
$this->mimeTypes = $allowedMimeTypes;
}
public function check($path)
{
return in_array(mime($path), $this->mimeTypes);
}
}
class FileSizeCheck implements FileCheck
{
private $maximumSize;
public function __construct($maximumSize)
{
$this->maximumSize = (int) $maximumSize;
}
public function check($path)
{
return filesize($path) <= $this->maximumSize;
}
}
$upload = new Upload();
$upload->addCheck(new FileSizeCheck(512kb));
$upload->addCheck(new MimeTypeCheck(array('application/xml')));
$upload->process($_FILES['taart']);
?>
class Upload
{
public function addCheck(FileCheck $check)
{
$this->checks[] = $check;
}
public function check($file)
{
// checkcheckcheckcheckcheckcheck
foreach($this->checks as $check)
if(!$check->check($file))
return false;
return true;
}
}
interface FileCheck
{
public function check($path);
}
class FileMimeCheck implements FileCheck
{
private $mimeTypes;
public function __construct(array $allowedMimeTypes)
{
$this->mimeTypes = $allowedMimeTypes;
}
public function check($path)
{
return in_array(mime($path), $this->mimeTypes);
}
}
class FileSizeCheck implements FileCheck
{
private $maximumSize;
public function __construct($maximumSize)
{
$this->maximumSize = (int) $maximumSize;
}
public function check($path)
{
return filesize($path) <= $this->maximumSize;
}
}
$upload = new Upload();
$upload->addCheck(new FileSizeCheck(512kb));
$upload->addCheck(new MimeTypeCheck(array('application/xml')));
$upload->process($_FILES['taart']);
?>