meerdere foto`s uploaden met overige informatie.

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Antony Riksen

Antony Riksen

23/07/2011 20:02:17
Quote Anchor link
Momenteel ben ik bezig met een upload script van een simpele webshop, maar het wil niet lukken,

Ik heb een upload sccript hiervandaan waarmee je meerdere foto`s kunt uploaden,

Voor deze pagina heb ik een pagina met input fielts:
-naam
-prijs
-omschrijving.

Deze worden opnieuw weergegeven op de fotoupload pagina,

als ik op upload druk plaatst die alles netjes in de DB echter
als ik 2 of meer foto`s upload krijg ik continu alleen de 1e te zien in de DB
en in de (ftp) map.

Graag hoor ik waar ik fout ga.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
//error_reporting(E_ALL ^ E_NOTICE);
error_reporting(E_ALL);
$naam = $_POST['naam'];
$prijs = $_POST['prijs'];
$tekst = $_POST['tekst'];

/**********************************************************************************
* Multifile files upload at once with a form,
* optional image resize for jpeg gif and png during upload
* Author: scripts[AT ][ .]titude.nl - NLD - 2007
* For use with resize Requires GD ( 2.0.28 or up for gif support )

# example .htaccess
php_value max_execution_time = 3600
php_value max_input_time = 3600
php_value post_max_size 18M
php_value upload_max_filesize 15M
**********************************************************************************/

$m_cnfg['max_size'] = 0; // size per file in bytes - 0 is unlimited
$m_cnfg['max_files'] = 6; // max files to upload - 0 is unlimited
$m_cnfg['ext_oke'] = array('png','jpg','gif','bmp','ttf','pdf');  // allowed extensions, empty allow all
// $m_cnfg['ext_oke'] = array('png','jpg','gif');

$m_cnfg['upload_dir'] = $_SERVER['DOCUMENT_ROOT'] . "/cms/foto"; // upload dir
$m_cnfg['overwrite'] = false; // allow files overwrite on true
$m_cnfg['newname'] = ""; // file rename "" or "timestamp" or set a rename prefix
$m_cnfg['inputname'] = "bestanden"; // input file form array name
$m_cnfg['imggd'] = array('jpg'); // resize with GD
$m_cnfg['imgw'] = 800; // max width for resize - 0 is no resize
$m_cnfg['imgh'] = 600; // max height for resize - 0 is no resize  


$m_log = multiup($m_cnfg);

function
multiup($m_cnfg) {
    $upmsg = array();
    if ($m_cnfg['max_size'] != 0) {
        $maxdisp = ($m_cnfg['max_size'] >= 1024000) ? round($m_cnfg['max_size'] / 10240) / 100 . " Mb" : round($m_cnfg['max_size'] / 10.24) / 100 . " Kb";
        $upmsg['rules'][] = "Max file size: " . $maxdisp;
    }

    if (!empty($m_cnfg['ext_oke'])) {
        $upmsg['rules'][] = "File types: " . implode(" ,", $m_cnfg['ext_oke']);
    }

    if ($m_cnfg['max_files'] != 0) {
        // $upmsg['rules'][] = "Simultanious: " . $m_cnfg['max_files'];
    }
    if ($_SERVER["REQUEST_METHOD"] == 'POST' && !empty($_FILES[$m_cnfg['inputname']]['name'])) {
        $naam = $_POST['naam'];
        $prijs = $_POST['prijs'];
        $tekst = $_POST['tekst'];
        if (is_writable($m_cnfg['upload_dir'])) {
            $filedir = str_replace("//", "/", $m_cnfg['upload_dir'] .  "/");
            $gdtrue = extension_loaded('gd') ? true : false;
            $gd201 = function_exists('imagecopyresampled') ? true : false;
            $gd2082 = $gd201 && function_exists('imagecreatefromgif') ? true : false;
            $maxup = 1;
            while(list($filenum, $filename) = each($_FILES[$m_cnfg['inputname']]['name'])) {
                $uptrue = false;
                if (!empty($filename)) {
                    $fileparts = pathinfo($filename);
                    $fileext = strtolower($fileparts['extension']);
                    // filter filename - adjust to needs - replace spaces by
                    // _ a-z A-Z - 0-9 _ . allowed - extension length 2-5 chars

                    $filename = str_replace("%20", "_",  str_replace(" ", "_",  $filename));
                    $uptrue = preg_match("/^[-a-z0-9_\.]+\.[a-z0-9]{2,5}$/i",  $filename) ? true : false;
                    $uptrue = $uptrue && $m_cnfg['max_files'] != 0 && $m_cnfg['max_files'] < $maxup ? false : true;
                    $uptrue = $uptrue && (($fileext != "" && in_array($fileext, $m_cnfg['ext_oke'])) || empty($m_cnfg['ext_oke'])) ? true : false;
                    $uptrue = $uptrue && ($_FILES['bestanden']['size'][$filenum] < $m_cnfg['max_size'] || $m_cnfg['max_size'] == 0) ? true : false;
                    if (!$uptrue) {
                        $upmsg['errors'][] = $filename;
                    }
                }

                if ($uptrue) {
                    $maxup++;
                    $newname = "";
                    if ($m_cnfg['overwrite'] !== true || $m_cnfg['newname'] != "") {
                        $tmpname = $m_cnfg['newname'] != "" ? $m_cnfg['newname'] : str_replace ("." . $fileext, "", $filename);
                        if ($m_cnfg['newname'] == "timestamp") {
                            list($usec) = explode(" ", microtime());
                            $tmpname = date('YmdHis') . (string)substr($usec,2,6);
                        }

                        $fileloc = $filedir . $tmpname . "." . $fileext;
                        $newname = $tmpname . "." . $fileext;
                        $a = 1;
                        if ($m_cnfg['overwrite'] !== true) {
                            while (file_exists($fileloc)) {
                                $a++;
                                $fileloc = $filedir . $tmpname . "_" . $a . "." . $fileext;
                                $newname = $tmpname . "_" . $a . "." . $fileext;
                            }
                        }
                    }
else {
                        $fileloc = $filedir . $filename;
                    }

                    if (move_uploaded_file($_FILES[$m_cnfg['inputname']]['tmp_name'][$filenum], $fileloc)) {
                        if ($newname == $filename || $newname == "") {
                            $upmsg['uploads'][] = $filename;
                            
// plaats een record in database.

$query="INSERT INTO verkoop (filename, type, title, prijs) VALUES ('".$filename."', 'algemeen', '".$naam."', '".$prijs."')";
$result=mysql_query($query);                
                            
                        }
else {
                            $upmsg['uploads'][] = $filename . " <B>ALS</B> " . $newname;
// plaats een record in database.

$query="INSERT INTO verkoop (filename, type, title, prijs) VALUES ('".$newname."', 'algemeen', '".$naam."', '".$prijs."')";
$result=mysql_query($query);
  
                        }

                        if ($gdtrue && in_array($fileext, $m_cnfg['imggd']) && $m_cnfg['imgw'] > 0 && $m_cnfg['imgh'] > 0) {
                            list($curw, $curh) = getimagesize($fileloc);
                            $p_x = $curw / $m_cnfg['imgw'];
                            $p_y = $curh / $m_cnfg['imgh'];
                            if ($p_x > 1 || $p_y > 1) {
                                $pfactor = ($p_x > $p_y) ? $pfactor = $p_x : $pfactor = $p_y;
                                $neww = round($curw / $pfactor);
                                $newh = round($curh / $pfactor);
                                $imgorg = false;
                                if ($fileext == "jpg" || $fileext =='jpeg') {
                                    $imgorg = imagecreatefromjpeg($fileloc);
                                }
else if ($gd2082 && $fileext == "gif") {
                                    $imgorg = imagecreatefromgif($fileloc);
                                }
else if ($fileext == 'png') {
                                    $imgorg = imagecreatefrompng($fileloc);
                                }

                                if ($imgorg) {
                                    if ($gd201) {
                                        $imgnew = imagecreatetruecolor($neww, $newh);
                                        if ($fileext == "gif" || $fileext == "png") {
                                            $bgcolor = imagecolorallocate($imgorg, 0, 0, 0);
                                            imagepalettecopy($imgnew, $imgorg);
                                            imagefill($imgnew, 0, 0, $bgcolor);
                                            imagecolortransparent($imgnew, $bgcolor);
                                        }

                                        imagecopyresampled($imgnew, $imgorg, 0, 0, 0, 0, $neww, $newh, $curw, $curh);
                                    }
else {
                                        $imgnew = imagecreate($neww, $newh);
                                        imagecopy($imgnew, $imgorg, 0, 0, 0, 0, $neww, $newh, $curw, $curh);
                                    }

                                    imagedestroy($imgorg);
                                    if ($fileext == 'jpg' || $fileext =='jpeg') {
                                        imagejpeg($imgnew, $fileloc, 80);
                                    }
else if ($gd2082 && $fileext == 'gif') {
                                        imagegif($imgnew, $fileloc);
                                    }
else if ($fileext == 'png') {
                                        imagepng($imgnew, $fileloc);
                                    }

                                    imagedestroy($imgnew);
                                    list($neww, $newh) = getimagesize($fileloc);
                                    array_pop($upmsg['uploads']);
                                    if ($newname == $filename || $newname == "") {
                                        $upmsg['uploads'][] = $filename . " - size: " . $neww . "px - " . $newh . "px";
                                    }
else {
                                        $upmsg['uploads'][] = $filename . " <B>ALS</B> " . $newname . " - New Size: " . $neww . "px - " . $newh . "px";
                                    }
                                }
                            }
                        }
                    }
                }

                if (file_exists($_FILES[$m_cnfg['inputname']]['tmp_name'][$filenum])) {
                    unlink($_FILES[$m_cnfg['inputname']]['tmp_name'][$filenum]);
                }
            }
        }
else {
            $upmsg['errors'][] = "Dir error: " . $m_cnfg['upload_dir'];
        }
    }

    return $upmsg;
}

?>


<script type="text/javascript"><!--
// specify max_files to upload when used apart from upload multi
// 0 = unlimited - adjust in your config also
var max_files = <?php echo $m_cnfg['max_files']; ?>;
var count_files = 1;
function bestanden() {
    if (max_files == 0 || max_files > count_files) {
        var holder = document.getElementById('bestanden').getElementsByTagName('tr')[0].parentNode;
        var taginp = document.createElement('input');
        taginp.setAttribute('type','file');
        taginp.setAttribute('name','bestanden[]');
        var tagtr = document.createElement('tr');
        var tagtd = document.createElement('td');  
        var tagtr = document.createElement('tr');
        var tagtd = document.createElement('td');
        var tagspan = document.createElement('span');
        tagspan.style.cursor = 'pointer';
        tagspan.onclick = function() {
            this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
            count_files--;
        }
        
        
        tagspan.appendChild(document.createTextNode(' Verwijder '));
        tagtd.appendChild(taginp);
        tagtr.appendChild(tagtd);
        holder.appendChild(tagtr);
        tagtd.appendChild(tagspan);
        tagtr.appendChild(tagtd);
        holder.appendChild(tagtr);
        count_files++;
    }
}
//--></script>
<form method="POST" action="index.php?pagina=add2" enctype="multipart/form-data">

<table id="bestanden">
<div>
    <input type="text" name="naam" value="<?php echo $naam; ?>"><br />
    <input type="text" name="prijs" value="<?php echo $prijs; ?>"><br />
    <input type="text" name="tekst" value="<?php echo $tekst; ?>"><br />
</div>

<tr><td><input type="file" name="bestanden[]"><span onClick="bestanden()" style="cursor:pointer;"><img src="cms/plus.png" alt="Voeg meer foto`s toe."></span></td></tr>
</table>
<input type="submit" value="Upload">
</form>
<?php
if (!empty($m_log['rules'])) {
    echo "<b>Upload rules</b>  <br />";
    echo implode("<br />", $m_log['rules']);
}

if (!empty($m_log['uploads'])) {
    echo "<br /><br /><b>Uploaded</b>  <br />";
    echo implode("<br />", $m_log['uploads']);
}

if (!empty($m_log['errors'])) {
    echo "<br /><br /><b>Upload failed for</b> <br />";
    echo implode("<br />", $m_log['errors']);
}

?>
 
PHP hulp

PHP hulp

26/12/2024 02:42:29
 
Antony Riksen

Antony Riksen

27/07/2011 11:06:42
Quote Anchor link
Hier kan een slotje op..
inmiddels een ander systeem aan het proberen
Gewijzigd op 27/07/2011 12:48:54 door Antony Riksen
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.