johnwayne77 Posted August 16, 2008 Share Posted August 16, 2008 i have an image upload system for a travel agency. the form has 4 file fields where u can browse and add your images for upload. when you press upload, the script automatically uploads each file, creates a thumbnail and then add the path to mysql. the problem is that i can't add all the files to mysql, just the first one. i believe i need a simillar loop or something.. here are parts of the code: for($i=0; $i<10; $i++) if(!empty($_FILES["upload$i"]['name'])) { $pic[$i] = $_FILES["upload$i"]['name']; } $totalPics = count($pic); for($i = 0; $i<$totalPics; $i++) if(is_uploaded_file($_FILES["upload$i"]['tmp_name']) and copy($_FILES["upload$i"]['tmp_name'], "$fileName$i$ext")) then it comes the INSERT sql command: $sql2 = "INSERT INTO images SET numestatiune='$numestatiune', filename='$fileName$i$ext', width='$newwidth', height='$newheight' "; if(mysql_query($sql2)) { echo "images uploaded and resized"; } so on the webserver i have 0.jpg , 1.jpg, 2.jpg but in sql i have only 0.jpg any ideas anyone? cheers Quote Link to comment https://forums.phpfreaks.com/topic/119969-array-loop/ Share on other sites More sharing options...
johnwayne77 Posted August 16, 2008 Author Share Posted August 16, 2008 i was thinking somehting like this: $xxdata = array(); foreach(array_keys($_FILES['upload$i']) as $i) $xxdata[] = $_FILES['upload$i'][$i]; $xxvalue = mysql_real_escape_string(implode("\n",$xxdata)); and then add the $xxvalue into sql but it doesn't work Quote Link to comment https://forums.phpfreaks.com/topic/119969-array-loop/#findComment-618008 Share on other sites More sharing options...
Fadion Posted August 16, 2008 Share Posted August 16, 2008 If u have 4 file uploads, lets say name "file1", "file2", "file3", "file4", u can use a similar code: <?php for($i=1;$i<=4;$i++){ $filename = $_FILES['file' . $i]['name']; $results = @mysql_query("INSERT INTO table (image) VALUES ('$filename')") or die(); } ?> EDIT: Noted that i had an extra loop in the snippet so i edited the code. Quote Link to comment https://forums.phpfreaks.com/topic/119969-array-loop/#findComment-618009 Share on other sites More sharing options...
johnwayne77 Posted August 16, 2008 Author Share Posted August 16, 2008 i've tried this but no luck for($i=1;$i<=4;$i++){ $fx = '$fileName' . $_FILES['upload' . $i] . '$ext'; } Quote Link to comment https://forums.phpfreaks.com/topic/119969-array-loop/#findComment-618016 Share on other sites More sharing options...
johnwayne77 Posted August 16, 2008 Author Share Posted August 16, 2008 anyone any ideaS? Quote Link to comment https://forums.phpfreaks.com/topic/119969-array-loop/#findComment-618026 Share on other sites More sharing options...
Barand Posted August 16, 2008 Share Posted August 16, 2008 How about the section in the manual that deals with multiple uploads http://www.php.net/manual/en/features.file-upload.multiple.php Quote Link to comment https://forums.phpfreaks.com/topic/119969-array-loop/#findComment-618069 Share on other sites More sharing options...
Stephen Posted August 16, 2008 Share Posted August 16, 2008 Could use this: $_pics = ""; $_pics_temp = ""; $_done = 0; foreach ($_FILES as $_key => $_value) { if(!empty($_FILES[$_key]['name'])) { $_pics .= $_FILES[$_key]['name'].","; $_pics_temp .= $_FILES[$_key]['tmp_name'].","; } } $_pics = explode(",", substr($_pics, 0, strlen($_pics)-1)); $_pics_temp = explode(",", substr($_pics_temp, 0, strlen($_pics_temp)-1)); $totalPics = count($_pics); foreach($_pics_temp as $_key => $_value) { $_rand = rand(0, 1000); if (is_uploaded_file($_value) && copy($_value, "$fileName$_rand$ext")) { $_sql = sprintf("INSERT INTO images (namestatiune, filename, width, height) VALUES ('%s', '%s', '%d', '%d')", $numestatiune, $fileName.$_rand.$ext, $newwidth, $newheight); if (mysql_query($_sql)) { $_done++; } } } echo("You have uploaded -".$_done."- image(s)."); I haven't really tested it though... Quote Link to comment https://forums.phpfreaks.com/topic/119969-array-loop/#findComment-618099 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.