bestanden uploaden
Vervolgens worden alle gegevens in de database gezet.
Maar het probleem is dat de array $_FILES['files'] geen waardes bevat?
de code waar het om gaat :
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
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
<?php
public function create_new_JPGimage ($breedte, $hoogte, $imgFILE, $imgDIR, $imgNAME, $imgEXTENSION, $quality)
{
//temp_photo voor thumbnail maken
$photo = imagecreatefromjpeg ($imgFILE);
//afmetingen bepalen
$source_x = imagesx($photo);
$source_y = imagesy($photo);
$target_x = $breedte;
$target_y = $hoogte;
//als foto te hoog is
if (($source_x/$target_x) < ($source_y/$target_y))
{
//echo "te hoog";
$from_y = ceil(($source_y - ($target_y * $source_x / $target_x))/2);
$from_x = '0';
$source_used_y = ceil(($target_y * $source_x / $target_x));
$source_used_x = $source_x;
}
//als foto te breed is
if (($source_y/$target_y) < ($source_x/$target_x))
{
//echo "te breed";
$from_x = ceil(($source_x - ($target_x * $source_y / $target_y))/2);
$from_y = '0';
$source_used_x = ceil(($target_x * $source_y / $target_y));
$source_used_y = $source_y;
}
//als verhoudingen gelijk zijn
if (($source_x/$target_x) == ($source_y/$target_y))
{
$from_x = '0';
$from_y = '0';
$source_used_x = $source_x;
$source_used_y = $source_y;
}
//nieuwe image maken en wegschrijven naar dir
$create_blank = imagecreatetruecolor ($target_x, $target_y);
imagecopyresampled ($create_blank, $photo, 0, 0, $from_x, $from_y, $target_x, $target_y, $source_used_x, $source_used_y);
$new_filename = $imgDIR.$imgNAME.$imgEXTENSION;
ImageJpeg($create_blank,$new_filename,$quality);
Imagedestroy($photo);
return true;
}
public function imageupload($file){
$dir = 'image_upload/';
$extention = explode('.', $file);
$allow[0] = "pjpeg";
$allow[1] = "gif";
$allow[2] = "png";
$allow[3] = "pjpg";
$allow[4] = "jpg";
$allow[5] = "jpeg";
$allow[6] = "x-png";
$location = 'image_upload/';
$allowed = FALSE;
foreach ( $allow AS $key => $value ){
if($allowed != true){
$allowed = ($value == $extention[1])?true:false;
}
}
if($allowed == true){
if(!file_exists($dir.$file)){
if(is_uploaded_file($dir.$file)){
if(move_uploaded_file($dir.$file, $dir.$file)){
return $dir.$file;
}else{
echo 'couldn\'t move the picture.';
exit();
}
}else{
echo 'couldn\'t upload the file';
exit();
}
}else{
echo 'file does all ready exists';
exit();
}
}else{
echo 'Wrong file type';
exit();
}
}
public function EditMessage(){
if(isset($_POST['editmessage'])){
global $_FILES;
$message = $_POST['bericht'];
$MessageId = $_POST['id'];
$path = $_post['files'];
$title = $_POST['title'];
if(!empty($_POST['files'])){
$path = $this->imageupload($path);
$breedt = 75;
$hoogte = 75;
$imgFILE = $path;
$imgNAME = md5(rand(0,1000000).date("NOW"));
$imgEXTENSION = ".jpg";
$quality = 75;
$imgDIR = "image_upload";
if(create_new_JPGimage ($breedte, $hoogte, $imgFILE, $imgDIR, $imgNAME, $imgEXTENSION, $quality) ){
if(mysql_query("UPDATE `NewsMessage` SET Message = '".$message."', Title ='".$title."', PathImg = 'image_upload/".$imgNAME.".jpg' WHERE MessageId = '".$MessageId."' ")){
$data .= $this->ReadMessage();
}
}
}else{
if(mysql_query("UPDATE `NewsMessage` SET NewsMessage = '".$message."', Title ='".$title."' WHERE NewsMessageId = '".$MessageId."' ")){
$data .= $this->ReadMessage();
}
}
}else{
$MessageId = $_GET['messageid'];
$qry = mysql_query("SELECT * FROM `NewsMessage` WHERE NewsMessageId = '".$MessageId."' AND (UserId = '".$_SESSION['userid']."' or ".$_SESSION['authlevel']." = '1')")or die(mysql_error());
while($row = mysql_fetch_object($qry)){
$mess = str_replace("<br />", "", $row->NewsMessage);
$data .= "
<br>\n
Edit your message :
<form enctype=\"multipart/form-data\" action='?mode=NewsMessage&type=edit' method='post' name='formulier'>\n
\t <input type ='text' name ='title' value ='".$row->Title."'><br>\n
\t <input type ='file' name ='files' > New picture\n
\t<textarea name=\"bericht\" cols=\"52\" rows=\"22\" class=\"input\">".$mess."</textarea><br>\n
\t<input type='hidden' name='id' value='".$row->NewsMessageId."'>
\t<input type='submit' name='editmessage' value='Edit'>\n
</form>\n";
}
}
return $data;
}
?>
public function create_new_JPGimage ($breedte, $hoogte, $imgFILE, $imgDIR, $imgNAME, $imgEXTENSION, $quality)
{
//temp_photo voor thumbnail maken
$photo = imagecreatefromjpeg ($imgFILE);
//afmetingen bepalen
$source_x = imagesx($photo);
$source_y = imagesy($photo);
$target_x = $breedte;
$target_y = $hoogte;
//als foto te hoog is
if (($source_x/$target_x) < ($source_y/$target_y))
{
//echo "te hoog";
$from_y = ceil(($source_y - ($target_y * $source_x / $target_x))/2);
$from_x = '0';
$source_used_y = ceil(($target_y * $source_x / $target_x));
$source_used_x = $source_x;
}
//als foto te breed is
if (($source_y/$target_y) < ($source_x/$target_x))
{
//echo "te breed";
$from_x = ceil(($source_x - ($target_x * $source_y / $target_y))/2);
$from_y = '0';
$source_used_x = ceil(($target_x * $source_y / $target_y));
$source_used_y = $source_y;
}
//als verhoudingen gelijk zijn
if (($source_x/$target_x) == ($source_y/$target_y))
{
$from_x = '0';
$from_y = '0';
$source_used_x = $source_x;
$source_used_y = $source_y;
}
//nieuwe image maken en wegschrijven naar dir
$create_blank = imagecreatetruecolor ($target_x, $target_y);
imagecopyresampled ($create_blank, $photo, 0, 0, $from_x, $from_y, $target_x, $target_y, $source_used_x, $source_used_y);
$new_filename = $imgDIR.$imgNAME.$imgEXTENSION;
ImageJpeg($create_blank,$new_filename,$quality);
Imagedestroy($photo);
return true;
}
public function imageupload($file){
$dir = 'image_upload/';
$extention = explode('.', $file);
$allow[0] = "pjpeg";
$allow[1] = "gif";
$allow[2] = "png";
$allow[3] = "pjpg";
$allow[4] = "jpg";
$allow[5] = "jpeg";
$allow[6] = "x-png";
$location = 'image_upload/';
$allowed = FALSE;
foreach ( $allow AS $key => $value ){
if($allowed != true){
$allowed = ($value == $extention[1])?true:false;
}
}
if($allowed == true){
if(!file_exists($dir.$file)){
if(is_uploaded_file($dir.$file)){
if(move_uploaded_file($dir.$file, $dir.$file)){
return $dir.$file;
}else{
echo 'couldn\'t move the picture.';
exit();
}
}else{
echo 'couldn\'t upload the file';
exit();
}
}else{
echo 'file does all ready exists';
exit();
}
}else{
echo 'Wrong file type';
exit();
}
}
public function EditMessage(){
if(isset($_POST['editmessage'])){
global $_FILES;
$message = $_POST['bericht'];
$MessageId = $_POST['id'];
$path = $_post['files'];
$title = $_POST['title'];
if(!empty($_POST['files'])){
$path = $this->imageupload($path);
$breedt = 75;
$hoogte = 75;
$imgFILE = $path;
$imgNAME = md5(rand(0,1000000).date("NOW"));
$imgEXTENSION = ".jpg";
$quality = 75;
$imgDIR = "image_upload";
if(create_new_JPGimage ($breedte, $hoogte, $imgFILE, $imgDIR, $imgNAME, $imgEXTENSION, $quality) ){
if(mysql_query("UPDATE `NewsMessage` SET Message = '".$message."', Title ='".$title."', PathImg = 'image_upload/".$imgNAME.".jpg' WHERE MessageId = '".$MessageId."' ")){
$data .= $this->ReadMessage();
}
}
}else{
if(mysql_query("UPDATE `NewsMessage` SET NewsMessage = '".$message."', Title ='".$title."' WHERE NewsMessageId = '".$MessageId."' ")){
$data .= $this->ReadMessage();
}
}
}else{
$MessageId = $_GET['messageid'];
$qry = mysql_query("SELECT * FROM `NewsMessage` WHERE NewsMessageId = '".$MessageId."' AND (UserId = '".$_SESSION['userid']."' or ".$_SESSION['authlevel']." = '1')")or die(mysql_error());
while($row = mysql_fetch_object($qry)){
$mess = str_replace("<br />", "", $row->NewsMessage);
$data .= "
<br>\n
Edit your message :
<form enctype=\"multipart/form-data\" action='?mode=NewsMessage&type=edit' method='post' name='formulier'>\n
\t <input type ='text' name ='title' value ='".$row->Title."'><br>\n
\t <input type ='file' name ='files' > New picture\n
\t<textarea name=\"bericht\" cols=\"52\" rows=\"22\" class=\"input\">".$mess."</textarea><br>\n
\t<input type='hidden' name='id' value='".$row->NewsMessageId."'>
\t<input type='submit' name='editmessage' value='Edit'>\n
</form>\n";
}
}
return $data;
}
?>
Heeft iemand een idee waarom de waarde van de array $_FILES leeg blijft?
Graag uw hulp dan kan ik weer verder:)
Gewijzigd op 01/01/1970 01:00:00 door Cyberboy fdsafds
Je benaming files is wel vreemd, want je hebt mee 1 input type file. Ik zou hem dus userfile of zoiets noemen.
Om te testen kun je verder nog dit doen: