c_pattle Posted May 29, 2010 Share Posted May 29, 2010 I'm trying to create a script to send multiple attachments with one email. I'm using an array to store the attachments and then trying to use a foreach loop to email them. However for some reason my array seems to be empy. This is my html <form method="post" action="order_success2.php" enctype="multipart/form-data"> <ul> <li><input type="file" name="att[]" size="26" /></li> <li><input type="file" name="att[]" size="26" /></li> <li><input type="submit" name="submit" value="Submit!" /></li> </ul> </form> And this is my php code foreach($_FILES['att'] as $key => $value){ $att_path = $value['tmp_name']; $att_name = $value['name']; $att_size = $value['size']; $att_type = $value['type']; $fp = fopen( $att_path, "rb"); $file = fread( $fp, $att_size ); fclose ($fp); Quote Link to comment https://forums.phpfreaks.com/topic/203270-array-not-working/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 29, 2010 Share Posted May 29, 2010 Your code has no error checking logic in to to test if the upload worked or failed. You must always test for any possible errors and validate data before you attempt to reference that data. Ref: http://us.php.net/manual/en/features.file-upload.errors.php Also see Example #3 at the following link for an example of code that at least tests the ['error'] element of the uploaded data - http://us.php.net/manual/en/features.file-upload.post-method.php Quote Link to comment https://forums.phpfreaks.com/topic/203270-array-not-working/#findComment-1064991 Share on other sites More sharing options...
c_pattle Posted May 29, 2010 Author Share Posted May 29, 2010 I used error checking to check if the the variables $value['tmp_name'] etc had anything in them and they came back as empty however I deleted this code to simplify it when copying it to here. I also used the following code to check the contents of the array and everything seem find so there must be something wrong in my foreach loop. Hope that makes sense. if (isset($_POST['submit'])) { echo '<pre>' . print_r($_FILES,true) . '</pre>'; } Quote Link to comment https://forums.phpfreaks.com/topic/203270-array-not-working/#findComment-1064995 Share on other sites More sharing options...
PFMaBiSmAd Posted May 29, 2010 Share Posted May 29, 2010 Please see the Example #3 code in the php.net documentation. The array you are getting by using name="att[]" isn't exactly what your code is accessing (use print_r() to see what is in $values.) Quote Link to comment https://forums.phpfreaks.com/topic/203270-array-not-working/#findComment-1064999 Share on other sites More sharing options...
c_pattle Posted May 29, 2010 Author Share Posted May 29, 2010 I'm still not sure why my code isn't working. Does anyone know what changes I need to make to my code for it to work? Quote Link to comment https://forums.phpfreaks.com/topic/203270-array-not-working/#findComment-1065006 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.