upload class fout
Ik gebruik voor het uploaden van mijn plaatjes/foto's Upload.class.php
Dit werkt perfect met foto's van mijn iphone.
Nu als in een foto van unsplash.com download krijg ik de melding Dit bestands type is niet toegestaan.
En dit is een gewone jpg.
upload.php
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
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
// als er op submit werd gedrukt
if(isset($_POST['naam'])){
include("Upload.class.php"); // voegt de class toe aan de code
// hier wordt de foto geupload zonder de resize functie te gebruiken
// de foto wordt opgeslagen in de : uploads_normale map
$upload_t = new upload_class("foto","../data/"); # hier wordt opgegeven welke path de foto moet komen
$upload_t->resize = true; // maakt de resize functie actief in de class
$upload_t->resizeType = 1; // resize op basis van width (max breedte)
$upload_t->width = 464; // breedte van het plaatje * dit is aanpasbaar *
$upload_t->height = 464; // hoogte van het plaatje * dit is aanpasbaar *
$upload_t->file();
// hier wordt de thumbnail opgeslagen in de maop : upload_thumbnail
$upload = new upload_class("foto","../data/"); // geeft aan hoe heet foto veld heet, en waar de foto heen moet
$upload->resize = true; // maakt de resize functie actief in de class
// toevoeging
$upload->resizeType = 1; // resize op basis van width (max breedte)
$upload->width = 64; // breedte van het plaatje * dit is aanpasbaar *
$upload->height = 64; // hoogte van het plaatje * dit is aanpasbaar *
$upload->file();
if($upload->name)
{
$file = $upload->name
?>
if(isset($_POST['naam'])){
include("Upload.class.php"); // voegt de class toe aan de code
// hier wordt de foto geupload zonder de resize functie te gebruiken
// de foto wordt opgeslagen in de : uploads_normale map
$upload_t = new upload_class("foto","../data/"); # hier wordt opgegeven welke path de foto moet komen
$upload_t->resize = true; // maakt de resize functie actief in de class
$upload_t->resizeType = 1; // resize op basis van width (max breedte)
$upload_t->width = 464; // breedte van het plaatje * dit is aanpasbaar *
$upload_t->height = 464; // hoogte van het plaatje * dit is aanpasbaar *
$upload_t->file();
// hier wordt de thumbnail opgeslagen in de maop : upload_thumbnail
$upload = new upload_class("foto","../data/"); // geeft aan hoe heet foto veld heet, en waar de foto heen moet
$upload->resize = true; // maakt de resize functie actief in de class
// toevoeging
$upload->resizeType = 1; // resize op basis van width (max breedte)
$upload->width = 64; // breedte van het plaatje * dit is aanpasbaar *
$upload->height = 64; // hoogte van het plaatje * dit is aanpasbaar *
$upload->file();
if($upload->name)
{
$file = $upload->name
?>
Upload.class.php
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?
class upload_class
{
var $field_name;
var $target_path;
var $name;
var $resize = false;
var $resizeType = 1; // 0 = vaste maten, 1 = variabel
var $width;
var $height;
var $type;
var $custom_types;
function upload_class($field,$target)
{
$this->target_path = $target;
$this->field_name = $field;
}
function file()
{
if($this->php_check() == true)
{
if($this->file_check() == true)
{
$this->move();
}
else
{
echo "<script>alert('Dit bestands type is niet toegestaan')</script>";
}
}
else {
echo "<script>alert('Het is niet toegestaan om PHP bestanden te uploaden !')</script>";
}
}
function move()
{
$input_name = $_FILES[$this->field_name]['name'];
$file_name = $this->genRandomString().$name.rand(5, 15).rand(5, 15).rand(5, 15).rand(5, 15).strstr($input_name, '.');
$this->name = $this->target_path . $file_name;
#if(move_uploaded_file($_FILES[$this->field_name]['tmp_name'], $this->name))
if(copy($_FILES[$this->field_name]['tmp_name'], $this->name))
{
$this->name = str_replace("../","",$this->name); // verwijderd de directory tags
if($this->resize == true)
{
$this->makeimage($this->name);
}
}
else
{
echo "Er is een fout opgetreden bij het uploaden van het bestand probeer later nog eens<br/>";exit;
}
}
function php_check()
{
$clean = true;
$blocklijst = array(".php",".php5",".xhtml",".dhtml",".PHP",".php3");
foreach ($blocklijst as $item)
{
if(preg_match("/$item\$/i", $_FILES[$this->field_name]['name']))
{
$clean = false;
}
}
return $clean;
}
function file_check()
{
$allowed = array("image/jpeg", "image/JPG", "image/JPEG", "image/JPG", "image/jpg", "image/pjpeg", "image/gif","image/png","image/tif");
#$imageinfo = getimagesize($_FILES[$this->field_name]['tmp_name']);
$header = $_FILES[$this->field_name]['type'];
# if (in_array($imageinfo['mime'], $allowed))
# {
if (in_array($header, $allowed))
{
return true;
}
else {
return false;
}
#} else {
# return false;
# }
}
function genRandomString() {
$length = 10;
$characters = "0123456789abcdefghijklmnopqrstuvwxyz";
$string = "";
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}
return $string;
}
function makeimage($filename)
{
// hier wordt gecontrolleerd of filename ../ directory tags bevat
// anders wordt de image type niet correct gepakt
#copy("uploads_normale/rrpusrd0xo712146.jpg", "upload_thumbnail/rrpusrd0xo712146.jpg");
if (strpos($filename, "../") !== false) {
$edit_filename = str_replace("../","",$filename); // verwijderd de directory tags
$image_type = strstr($edit_filename, '.'); // pakt vervolgens de image type
}
else
{
$image_type = strstr($filename, '.');
}
switch($image_type) {
case '.jpg':
$source = imagecreatefromjpeg($filename);
break;
case '.JPG':
$source = imagecreatefromjpeg($filename);
break;
case '.jpeg':
$source = imagecreatefromjpeg($filename);
break;
case '.png':
$source = imagecreatefrompng($filename);
break;
case '.gif':
$source = imagecreatefromgif($filename);
break;
case '.tif':
$source = imagecreatefromgif($filename);
break;
default:
echo("Error Invalid Image Type ");
die;
break;
}
$fullpath = $filename;
list($org_width, $org_height) = getimagesize($fullpath);
if ($this->resizeType == 0)
{
// resize op vaste maten
$newX = $this->width;
$newY = $this->height;
}
else
{
$percentage = ($org_width > $org_height) ? ($this->width / $org_width) : ($this->width / $org_height);
$newX = round($org_width * $percentage);
$newY = round($org_height * $percentage);
}
$thumb = imagecreatetruecolor($newX, $newY);
if(($image_type == ".gif") or ($image_type == ".png"))
{
// Indien het om een gif of png bestand gaat zal deze extra gerenderd worden
// zodat het plaatje niet automatisch een zwarte achtergrond krijgt.
imagealphablending($thumb, false);
imagesavealpha($thumb,true);
$transparent = imagecolorallocatealpha($thumb, 255, 255, 255, 127);
imagefilledrectangle($thumb, 0, 0, $newX, $newY, $transparent);
}
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newX, $newY, $org_width, $org_height);
imagejpeg($thumb, $fullpath, 100);
}
}
?>
class upload_class
{
var $field_name;
var $target_path;
var $name;
var $resize = false;
var $resizeType = 1; // 0 = vaste maten, 1 = variabel
var $width;
var $height;
var $type;
var $custom_types;
function upload_class($field,$target)
{
$this->target_path = $target;
$this->field_name = $field;
}
function file()
{
if($this->php_check() == true)
{
if($this->file_check() == true)
{
$this->move();
}
else
{
echo "<script>alert('Dit bestands type is niet toegestaan')</script>";
}
}
else {
echo "<script>alert('Het is niet toegestaan om PHP bestanden te uploaden !')</script>";
}
}
function move()
{
$input_name = $_FILES[$this->field_name]['name'];
$file_name = $this->genRandomString().$name.rand(5, 15).rand(5, 15).rand(5, 15).rand(5, 15).strstr($input_name, '.');
$this->name = $this->target_path . $file_name;
#if(move_uploaded_file($_FILES[$this->field_name]['tmp_name'], $this->name))
if(copy($_FILES[$this->field_name]['tmp_name'], $this->name))
{
$this->name = str_replace("../","",$this->name); // verwijderd de directory tags
if($this->resize == true)
{
$this->makeimage($this->name);
}
}
else
{
echo "Er is een fout opgetreden bij het uploaden van het bestand probeer later nog eens<br/>";exit;
}
}
function php_check()
{
$clean = true;
$blocklijst = array(".php",".php5",".xhtml",".dhtml",".PHP",".php3");
foreach ($blocklijst as $item)
{
if(preg_match("/$item\$/i", $_FILES[$this->field_name]['name']))
{
$clean = false;
}
}
return $clean;
}
function file_check()
{
$allowed = array("image/jpeg", "image/JPG", "image/JPEG", "image/JPG", "image/jpg", "image/pjpeg", "image/gif","image/png","image/tif");
#$imageinfo = getimagesize($_FILES[$this->field_name]['tmp_name']);
$header = $_FILES[$this->field_name]['type'];
# if (in_array($imageinfo['mime'], $allowed))
# {
if (in_array($header, $allowed))
{
return true;
}
else {
return false;
}
#} else {
# return false;
# }
}
function genRandomString() {
$length = 10;
$characters = "0123456789abcdefghijklmnopqrstuvwxyz";
$string = "";
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}
return $string;
}
function makeimage($filename)
{
// hier wordt gecontrolleerd of filename ../ directory tags bevat
// anders wordt de image type niet correct gepakt
#copy("uploads_normale/rrpusrd0xo712146.jpg", "upload_thumbnail/rrpusrd0xo712146.jpg");
if (strpos($filename, "../") !== false) {
$edit_filename = str_replace("../","",$filename); // verwijderd de directory tags
$image_type = strstr($edit_filename, '.'); // pakt vervolgens de image type
}
else
{
$image_type = strstr($filename, '.');
}
switch($image_type) {
case '.jpg':
$source = imagecreatefromjpeg($filename);
break;
case '.JPG':
$source = imagecreatefromjpeg($filename);
break;
case '.jpeg':
$source = imagecreatefromjpeg($filename);
break;
case '.png':
$source = imagecreatefrompng($filename);
break;
case '.gif':
$source = imagecreatefromgif($filename);
break;
case '.tif':
$source = imagecreatefromgif($filename);
break;
default:
echo("Error Invalid Image Type ");
die;
break;
}
$fullpath = $filename;
list($org_width, $org_height) = getimagesize($fullpath);
if ($this->resizeType == 0)
{
// resize op vaste maten
$newX = $this->width;
$newY = $this->height;
}
else
{
$percentage = ($org_width > $org_height) ? ($this->width / $org_width) : ($this->width / $org_height);
$newX = round($org_width * $percentage);
$newY = round($org_height * $percentage);
}
$thumb = imagecreatetruecolor($newX, $newY);
if(($image_type == ".gif") or ($image_type == ".png"))
{
// Indien het om een gif of png bestand gaat zal deze extra gerenderd worden
// zodat het plaatje niet automatisch een zwarte achtergrond krijgt.
imagealphablending($thumb, false);
imagesavealpha($thumb,true);
$transparent = imagecolorallocatealpha($thumb, 255, 255, 255, 127);
imagefilledrectangle($thumb, 0, 0, $newX, $newY, $transparent);
}
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newX, $newY, $org_width, $org_height);
imagejpeg($thumb, $fullpath, 100);
}
}
?>
Code (php)
1
2
3
2
3
<?php
$allowed = array("image/jpeg", "image/JPG", "image/JPEG", "image/JPG", "image/jpg", "image/pjpeg", "image/gif","image/png","image/tif");
?>
$allowed = array("image/jpeg", "image/JPG", "image/JPEG", "image/JPG", "image/jpg", "image/pjpeg", "image/gif","image/png","image/tif");
?>
Echo eens voor de snelle handigheid eens even $header in de function file_check() { .. } functie?
Dan weet je wat het wel is.
Onthoud wel dat dit een oude class is, die in PHP 7 uiteindelijk niet meer zal werken. (geen visibilty en vooral geen constructor).
Iets actueels kan je op Verot.net vinden.
Gewijzigd op 06/08/2019 01:38:43 door - Ariën -
Ik gebruik nu de source van vero en deze werkt perfect :)