ik heb hieronder een mooi scriptje voor het uplaoden van images zodat ze ook verkleind worden. het probleem is dat ik de naam van de document wil aanpassen naar $art_nr. helaas als ik de $file['name'] wijzig lijkt dat niet. iemand enig idee? HELP ME
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
| <?php $art_nr = $_POST['art_nr']; echo"$art_nr"; function validate_upload($file, $max_size, $allowed_types) { $output = 'ok'; $file_name = $file['name']; $file_type = $file['type']; $file_size = $file['size']; $file_tmp_name = $file['tmp_name']; $file_error = $file['error']; // http://www.php.net/manual/nl/features.file-upload.errors.php $error_msg = array( 0 => "There is no error, the file uploaded with success", 1 => "The uploaded file exceeds the upload_max_filesize (" . ini_get("upload_max_filesize") . "directive in php.ini", 2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", 3 => "Het bestand is slechts gedeeltelijk geupload", // The uploaded file was only partially uploaded 4 => "Geen bestand upgeload", // No file was uploaded 6 => "Geen tijdelijke map" // Missing a temporary folder ); // Als er geen fout is if($file_error == 0) { // Controleren of bestandstype wel toegelaten is if(in_array($file_type, $allowed_types)) { $output = 'ok'; } else { $output = "Bestandstype niet toegelaten"; } } else { // Toen de correcte foutmelding $output = $error_msg[$file_error]; // Geef foutmelding terug } return $output; } /** * Verplaats het up te laden bestand van de tijdelijke map naar het opgegeven pad * Resize wanneer het een plaatje is en wanneer nodig * * @param file $file * @param string $path * @param int $max_size * @param array $allowed_types * @param int $image_width * @param int $image_height * @param boolean $thumb * @param int $thumb_prefix * @param int $thumb_width * @param int $thumb_height * @return string */ function do_upload($file, $path, $max_size, $allowed_types, $image_width=false, $image_height=false, $thumb=false, $thumb_prefix=false, $thumb_width=false, $thumb_height=false) { $output = ''; // bestand valideren (bestandstype, grootte, ...) $output = validate_upload($file, $max_size, $allowed_types); if($output == 'ok') { // Upload het bestand if(move_uploaded_file($file["tmp_name"], $path . $file['name'])) { // Array met images, zodat we weten welke te resizen $images = array("image/jpeg", "image/pjpeg", "image/x-png"); // Is de file resize-baar ? if(in_array($file['type'], $images)) { switch ($file['type']) { case "image/jpeg": case "image/pjpeg": // Groot plaatje resizen resize_jpg($path . $file['name'], $image_width, $image_height); $output = 'ok'; // Thumb maken indien nodig if($thumb == true) { if(!$thumb_prefix) { $thumb_prefix = 'thumb_'; } $source = $path . $file['name']; $target = $path . $thumb_prefix . $file['name']; // File kopieren en resizen if(copy($source, $target)) { resize_jpg($path . $thumb_prefix . $file['name'], $thumb_width, $thumb_height); $output = 'ok'; } else { $output = 'Fout bij het creëeren van de thumbnail'; } } break; } } else { $output = 'ok'; } } else { $output = 'Fout bij het uploaden'; } } return $output; } /** * Resize de JPG indien nodig * * @param file $file * @param int $max_width * @param int $max_height */ function resize_jpg($file, $max_width, $max_height) { $resize = false; $size = getimagesize($file); $width = $size[0]; $height = $size[1]; $w = $width; $h = $height; if ($width > $height && $width > $max_width) { $h = $height / ($width / $max_width); $h = round($h); $w = $max_width; $resize = true; } elseif($height > $width && $height > $max_height) { $w = $width / ($height / $max_height); $w = round($w); $h = $max_height; $resize = true; } if($resize == true) { $thumb = imagecreatetruecolor($w, $h); $image = ImageCreateFromJpeg($file); $image_data = getimagesize($file); imagecopyresampled($thumb, $image, 0, 0, 0, 0, $w, $h, $image_data[0], $image_data[1]); imagejpeg($thumb, $file, 100); } } function list_uploads($path) { $output = ''; if ($dir = opendir($path)) { $output .= '<ul>'; while (($file = readdir($dir)) !== false) { if (($file != '.') and ($file != '..')) { if (!is_dir($path . $file)) { $output .= '<li>' . $file . '</a></li>'; } } } $output .= '</ul>'; } closedir($dir); return $output; } ?> |