mac007 Posted February 13, 2010 Share Posted February 13, 2010 Hello, all: I'm trying to learn and do a simple foreach loop so I can upload multiple files at once, but all I actually get in umy "picts" directory is an "Array" file... maybe I got the $key variable in wrong places? Thanks! <code> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Untitled Document</title> </head> <body> <?php if(isset($_FILES['picture']['tmp_name'])) { $fileArray = $_FILES['picture']['tmp_name']; foreach ($fileArray as $key) { move_uploaded_file($key,"picts/" . $_FILES['picture']['name']); } } ?> <form method="post" enctype="multipart/form-data"> <label> <input type="file" name="picture[]" id="picture[]" /> <br /> <input type="file" name="picture[]" id="picture[]" /> <br /> <input type="file" name="picture[]" id="picture[]" /> </label> <p> <input type="submit" name="button" id="button" value="Submit" /> </p> </form> </body> </html> </code> Link to comment https://forums.phpfreaks.com/topic/191974-cant-get-this-foreach-loop-to-upload-multiple-files-it-just-dumps-array-file/ Share on other sites More sharing options...
JAY6390 Posted February 13, 2010 Share Posted February 13, 2010 <?php if(isset($_FILES['picture'])) { $fileArray = $_FILES['picture']; foreach ($fileArray as $key) { move_uploaded_file($key['tmp_name'],"picts/" . $key['name']); } } ?> try that. your files are uploaded as an array, so each of the $_FILES['picture'] need looping before trying to access the temp name etc Link to comment https://forums.phpfreaks.com/topic/191974-cant-get-this-foreach-loop-to-upload-multiple-files-it-just-dumps-array-file/#findComment-1011853 Share on other sites More sharing options...
teamatomic Posted February 13, 2010 Share Posted February 13, 2010 Like it says in your file. $_FILES['picture']['name'] is an array $i=0; foreach ($fileArray as $key) { move_uploaded_file($key,"picts/" . $_FILES['picture']['name'][$i]); $i++; } HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/191974-cant-get-this-foreach-loop-to-upload-multiple-files-it-just-dumps-array-file/#findComment-1011856 Share on other sites More sharing options...
mac007 Posted February 13, 2010 Author Share Posted February 13, 2010 Thanks Teamatomic!... it work like a charm... I thin arrays are gonna take me right to the grave.. cant seem to ever get them right. I tried your fix Jay but that wasnt doing it... Thanks guys... Link to comment https://forums.phpfreaks.com/topic/191974-cant-get-this-foreach-loop-to-upload-multiple-files-it-just-dumps-array-file/#findComment-1011862 Share on other sites More sharing options...
JAY6390 Posted February 13, 2010 Share Posted February 13, 2010 Oops I did it the wrong way around lol Link to comment https://forums.phpfreaks.com/topic/191974-cant-get-this-foreach-loop-to-upload-multiple-files-it-just-dumps-array-file/#findComment-1011865 Share on other sites More sharing options...
mac007 Posted February 13, 2010 Author Share Posted February 13, 2010 well, thought I was done.. got one more question on this... how would I go about pulling the names of each file from the foreach loop so they can be written out to a database? do I need to unserialize them back out ? Link to comment https://forums.phpfreaks.com/topic/191974-cant-get-this-foreach-loop-to-upload-multiple-files-it-just-dumps-array-file/#findComment-1011882 Share on other sites More sharing options...
teamatomic Posted February 13, 2010 Share Posted February 13, 2010 just foreach the $_FILES['picture']['name'] array and insert. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/191974-cant-get-this-foreach-loop-to-upload-multiple-files-it-just-dumps-array-file/#findComment-1011884 Share on other sites More sharing options...
mac007 Posted February 13, 2010 Author Share Posted February 13, 2010 Thank you Team... I kind of get what you are saying, but thing is that this set of picts would belong to to 1 specific item/record... so I was trying to somehow get the each specific file name so that they in turn could be each inserted within that record's filename data-fields. Since I have 3 form fields and corresponding 3 database 'imagee" fields, I kind of did this, which I think should work, as I am now able to isolate each name and insert it accordingly... I know, very primitive and backwards, but was only I way I was able to rig it... <?php if(isset($_FILES['picture']['tmp_name'])) { $fileArray = $_FILES['picture']['tmp_name']; $i=0; foreach ($fileArray as $key) { move_uploaded_file($key,"picts/" . $_FILES['picture']['name'][$i]); $i++; } } $fileName1 = $_FILES['picture']['name'][0]; $fileName2 = $_FILES['picture']['name'][1]; $fileName3 = $_FILES['picture']['name'][2]; echo $fileName1; // each of these variables I can now insert... echo $fileName2; echo $fileName3; ?> Link to comment https://forums.phpfreaks.com/topic/191974-cant-get-this-foreach-loop-to-upload-multiple-files-it-just-dumps-array-file/#findComment-1011893 Share on other sites More sharing options...
teamatomic Posted February 13, 2010 Share Posted February 13, 2010 CONNECT/SELECT $array=$_FILES['picture']['name']; foreach($array as $img) { INSERT INTO blah...blah $img } HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/191974-cant-get-this-foreach-loop-to-upload-multiple-files-it-just-dumps-array-file/#findComment-1011898 Share on other sites More sharing options...
mac007 Posted February 13, 2010 Author Share Posted February 13, 2010 Thanks Team... gonna try that too! woudl be better definitely as it doesnt need me to pre-define variables... Thanks! Link to comment https://forums.phpfreaks.com/topic/191974-cant-get-this-foreach-loop-to-upload-multiple-files-it-just-dumps-array-file/#findComment-1011907 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.