tjodolv Posted September 23, 2007 Share Posted September 23, 2007 Hi. I'm trying to make a script upload several images at once. I have a form which creates an array of files, with title and description for each file. The following script is supposed to handle the files properly, one by one, but it stops after the first image. I've tried with different images, different filetypes and so on, and I am basically stuck. What am I missing? <?php $imageError = ""; foreach ($_FILES["image"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["image"]["tmp_name"][$key]; $tmp = strtolower($_FILES["image"]["name"][$key]); $illegalChars = "[\!|\?|\*|\||\&|\#|\^| |\,|\+|\$\(|\)|\[|\]|\{|\}]|%|/"; $tmp = ereg_replace($illegalChars, "_", $tmp); $langSpecificChars = array("Æ", "Ø", "Å", "æ", "ø", "å"); $english = array("e", "o", "a", "e", "o", "a"); $name = str_replace($langSpecificChars, $english, $tmp); if (($_FILES["image"]["type"][$key] != "image/jpeg") && ($_FILES["image"]["type"][$key] != "image/pjpeg") && ($_FILES["image"]["type"][$key] != "image/gif") && ($_FILES["image"]["type"][$key] != "image/png")) { // this is for weeding out non-images and then show which files that were not accepted $imageError .= $name . " "; continue; } $imgInfo = getimagesize($tmp_name); // images larger than 800x600 should be resized to save bandwidth/diskspace, and be nice to people with small monitors if (($imgInfo[0] > 800) || ($imgInfo[1] > 800)) { $tmq = $imgInfo[0]; if ($imgInfo[0] < $imgInfo[1]) { $tmq = $imgInfo[1]; } $tmp = $tmq / 800; $cropwidth = $imgInfo[0] / $tmp; $cropheight = $imgInfo[1] / $tmp; cropImage($cropwidth, $cropheight, $tmp_name, $_FILES["image"]["type"][$key], "../$page/$name"); } else { move_uploaded_file($tmp_name, "../$page/$name"); } // image resized (if needed) - moving on to the thumbnail $cropwidth = 150; // max thumbnail dimensions $cropheight = 150; if ($imgInfo[0] < $imgInfo[1]) { $cropwidth = 100; $cropheight = 150; } else { $cropwidth = 150; $cropheight = 100; } $thumbnail = "../$page/thumb_$name"; cropImage($cropwidth, $cropheight, "../$page/$name", $_FILES["image"]["type"][$key], $thumbnail); // image and thumbnail done, insert info into db $tmp = htmlspecialchars($_POST['imageTitle'][$key]); $imageTitle = mysql_escape_string($tmp); $tmp = htmlspecialchars($_POST['imgDescription'][$key]); $imageDescription = mysql_escape_string($tmp); $tmp = htmlspecialchars($_POST['approved']); $imageApproved = mysql_escape_string($tmp); $sql = "INSERT INTO ".$dbTablePrefix."_images (location, gallery, title, description, width, height, approved) VALUES ( " ."'".$name."', " ."'".$page."', " ."'".$imageTitle."', " ."'".$imageDescription."', " ."'".$imgInfo[0]."', " ."'".$imgInfo[1]."', " ."1" .")"; mysql_query($sql, $dbConnect); } // end if } // end foreach ?> Quote Link to comment https://forums.phpfreaks.com/topic/70402-solved-foreach-loop-stops-after-first-pass/ Share on other sites More sharing options...
BlueSkyIS Posted September 23, 2007 Share Posted September 23, 2007 you only reference one file: $_FILES["image"]["error"] $_FILES will contain a key for each <INPUT TYPE='FILE' NAME='$KEY'> in your form. You'll need to look at each uploaded image, not just the one called image. Quote Link to comment https://forums.phpfreaks.com/topic/70402-solved-foreach-loop-stops-after-first-pass/#findComment-353695 Share on other sites More sharing options...
teng84 Posted September 23, 2007 Share Posted September 23, 2007 can you post your version with this script <INPUT TYPE='FILE' NAME='$KEY'> Quote Link to comment https://forums.phpfreaks.com/topic/70402-solved-foreach-loop-stops-after-first-pass/#findComment-353697 Share on other sites More sharing options...
tjodolv Posted September 23, 2007 Author Share Posted September 23, 2007 I am not really sure what you mean... The thing is, i did this once before, this is basically just a translation of my previous script (had all the variables and stuff in Norwegian..) So it's a translated copy of a previous script, and the previous one works! I'll post some more code: Old stuff (form and script) - Norwegian... <p><input type="file" name="bilde[]" accept="image/jpeg" /></p> <p><input type="text" name="bildeTittel[]" maxlength="25" value="Tittel" /></p> <p style="margin-bottom: 15px;"><textarea name="beskrivelse[]" rows="2" cols="40">Beskrivelse...</textarea></p> <div id="myDiv"> </div> <input type="hidden" value="0" id="theValue" /> <p><a href="javascript:;" onclick="flereBilder();" title="Enda et bilde...">Nytt bilde...</a></p> <?php $feil = ""; $sidenavn = mysql_escape_string($_POST['sidenavn']); // vi har et array med filer. leser dem inn via en foreach-løkke foreach ($_FILES["bilde"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["bilde"]["tmp_name"][$key]; $tmp = strtolower($_FILES["bilde"]["name"][$key]); $ulovlig = "[\!|\?|\*|\||\&|\#|\^| |\,|\+|\$\(|\)|\[|\]|\{|\}]|%|/"; $tmp = ereg_replace($ulovlig, "_", $tmp); $norske = array("Æ", "Ø", "Å", "æ", "ø", "å"); $engelske = array("e", "o", "a", "e", "o", "a"); $name = str_replace($norske, $engelske, $tmp); if (($_FILES["bilde"]["type"][$key] != "image/jpeg") && ($_FILES["bilde"]["type"][$key] != "image/pjpeg") && ($_FILES["bilde"]["type"][$key] != "image/gif") && ($_FILES["bilde"]["type"][$key] != "image/png")) { $feil .= $name . " "; continue; } $bildeinfo = getimagesize($tmp_name); // må ha litt info om bildets dimensjoner til databasen if (($bildeinfo[0] > 800) || ($bildeinfo[1] > 800)) { $tmq = $bildeinfo[0]; if ($bildeinfo[0] < $bildeinfo[1]) { $tmq = $bildeinfo[1]; } $tmp = $tmq / 800; $cropbredde = $bildeinfo[0] / $tmp; $crophoyde = $bildeinfo[1] / $tmp; cropImage($cropbredde, $crophoyde, $tmp_name, $_FILES["bilde"]["type"][$key], "../$sidenavn/$name"); } else { move_uploaded_file($tmp_name, "../$sidenavn/$name"); } // så må vi lage et lite bilde til forhåndsvisningen $cropbredde = 150; // maks minibilde dimensjoner $crophoyde = 150; if ($bildeinfo[0] < $bildeinfo[1]) { $cropbredde = 100; $crophoyde = 150; } else { $cropbredde = 150; $crophoyde = 100; } $minibilde = "../$sidenavn/lite_$name"; // så lager vi det croppede bildet cropImage($cropbredde, $crophoyde, "../$sidenavn/$name", $_FILES["bilde"]["type"][$key], $minibilde); // filen er lastet opp og thumbnail er lagd, må legge inn info i databasen // fjerner skumle saker $tmp = htmlspecialchars($_POST['bildeTittel'][$key]); $bildeTittel = mysql_escape_string($tmp); $tmp = htmlspecialchars($_POST['beskrivelse'][$key]); $bildeBeskrivelse = mysql_escape_string($tmp); $tmp = htmlspecialchars($_POST['godkjent']); $bildeGodkjent = mysql_escape_string($tmp); $sql = "INSERT INTO " . $tabellPrefiks . "_bilder (adresse, galleri, tittel, beskrivelse, bredde, hoyde, godkjent) VALUES ( " . "'" . $name . "', " . "'" . $sidenavn . "', " . "'" . $bildeTittel . "', " . "'" . $bildeBeskrivelse . "', " . "'" . $bildeinfo[0] . "', " . "'" . $bildeinfo[1] . "', " . "$bildeGodkjent" . ")"; mysql_query($sql, $tilkobling); } // slutt if } // slutt foreach ?> This script works. The javascript stuff in the form is for adding more images to the form. I may just be too tired to see some translation error, but the funny thing is that that would probably break the whole script, and the first image always gets uploaded... Please help a poor n00b Off to bed, will look at it again tomorrow... Quote Link to comment https://forums.phpfreaks.com/topic/70402-solved-foreach-loop-stops-after-first-pass/#findComment-353711 Share on other sites More sharing options...
teng84 Posted September 24, 2007 Share Posted September 24, 2007 what i mean is that do u name your file field as array something like <INPUT TYPE='FILE' NAME='bilde[]'> because this script foreach ($_FILES["bilde"]["error"] as $key => $error) sounds like you do it that way Quote Link to comment https://forums.phpfreaks.com/topic/70402-solved-foreach-loop-stops-after-first-pass/#findComment-353719 Share on other sites More sharing options...
tjodolv Posted September 24, 2007 Author Share Posted September 24, 2007 Yep, as you can see in my second code example, the file input is named name="bilde[]" (or name="image[]" in the form for my first example). I am sure the solution is quite simple, but I just can't seem to find the faulty line(s)... Quote Link to comment https://forums.phpfreaks.com/topic/70402-solved-foreach-loop-stops-after-first-pass/#findComment-353929 Share on other sites More sharing options...
rarebit Posted September 24, 2007 Share Posted September 24, 2007 I had a similar problem, so I named all elements in the form e.g. ( 'item'.$i ). Then it worked fine, and I used... foreach($_FILES as $file_name => $file_array) Quote Link to comment https://forums.phpfreaks.com/topic/70402-solved-foreach-loop-stops-after-first-pass/#findComment-353931 Share on other sites More sharing options...
tjodolv Posted September 24, 2007 Author Share Posted September 24, 2007 Okay, so I decided to have another go at this... The problem seems to be that when $_POST is passed, for some reason the image array only gets passed with the first value. My code follows (exerpt). The form: <form action="" method="post" accept-charset="utf-8" enctype="multipart/form-data"> (...) <input type="hidden" name="approved" value="1" id="approved" /> <p><input type="file" name="image[]" accept="image/jpeg" /></p> <p><input type="text" name="imageTitle[]" maxlength="25" value="Title" /></p> <textarea name="imgDescription[]" rows="3" cols="40">Description...</textarea> <p><input type="button" name="more_images" value="Add another image" id="" onclick="moreImages()" /></p> (...) <p><input type="submit" name="new_page" value="Save" /></p> </form> I made the receiving script not do anything except a print_r($_POST); and print_r($_FILES); This gives me the following output (the whole form): <?php // $_POST Array ( [new_p_title] => Title [pri_cont] => Some content... [sec_cont] => 0 [tert_cont] => 0 [glist] => on [navigation] => on [img] => on [approved] => 1 [imageTitle] => Array ( [0] => Title ) [imgDescription] => Array ( [0] => Description... ) [new_page] => Save ) // $_FILES Array ( [image] => Array ( [name] => Array ( [0] => image_2.png ) [type] => Array ( [0] => image/png ) [tmp_name] => Array ( [0] => /Applications/MAMP/tmp/php/phpuLMpOu ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 352906 ) ) ) ?> As you can see, only the firs value gets passed, even though i have added more images in the array (the form is simply shortened). However, in the "old" form and script gives me "correct" arrays when i print_r($_POST); and print_r($_FILES); (the above is basically just a translation from the old, which has Norwegian variable names and all that stuff...) Output from old script (exerpt): <?php // $_POST [bildeTittel] => Array ( [0] => Tittel [1] => Tittel ) [beskrivelse] => Array ( [0] => Beskrivelse... [1] => Beskrivelse... ) ) //$_FILES Array ( [bilde] => Array ( [name] => Array ( [0] => Bilde 2.png [1] => Bilde 3.png ) [type] => Array ( [0] => image/png [1] => image/png ) [tmp_name] => Array ( [0] => /Applications/MAMP/tmp/php/phpJRzevK [1] => /Applications/MAMP/tmp/php/phpIQOhTz ) [error] => Array ( [0] => 0 [1] => 0 ) [size] => Array ( [0] => 352906 [1] => 208167 ) ) ) ?> Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/70402-solved-foreach-loop-stops-after-first-pass/#findComment-354432 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.