c_pattle Posted May 28, 2010 Share Posted May 28, 2010 I have a form with two attachment inputs <li><input type="file" name="att[0]" size="26" /></li> <li><input type="file" name="att[1]" size="26" /></li> I'm trying to write a script to send all attachments rather than just one so I'm trying to use a foreach loop but I'm getting this error - Warning: Invalid argument supplied for foreach() in /websites/123reg/LinuxPackage21/co/lo/ur/colourbrush.co.uk/public_html/order_success2.php. foreach($_POST['att'] as $key => $value){ $att = $value['att']; $att_path = $value['att']['tmp_name']; $att_name = $value['att']['name']; $att_size = $value['att']['size']; $att_type = $value['att']['type']; $fp = fopen( $att_path, "rb"); $file = fread( $fp, $att_size ); fclose ($fp); $num = md5(time()); $str = "==multipart_Boundary_x{$num}x"; $file = chunk_split(base64_encode($file)); Not sure what I'm doing wrong. Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/203213-for-each-loop/ Share on other sites More sharing options...
-Karl- Posted May 28, 2010 Share Posted May 28, 2010 print_r($_POST['att']); Make sure it's an array. Quote Link to comment https://forums.phpfreaks.com/topic/203213-for-each-loop/#findComment-1064726 Share on other sites More sharing options...
Maq Posted May 28, 2010 Share Posted May 28, 2010 </pre> <li> <b You're getting the error because you're not sending an array. Change the names to both be the same array with no key values. Quote Link to comment https://forums.phpfreaks.com/topic/203213-for-each-loop/#findComment-1064741 Share on other sites More sharing options...
kenrbnsn Posted May 28, 2010 Share Posted May 28, 2010 No, the OP is sending an array. You can include key values in the name attribute. That works fine. The problem is that the OP is looking for $_POST['att'] when the name "att" is type="file" and would in the $_FILES array. Here's a simple script that shows this: <?php if (isset($_POST['submit'])) { echo '<pre>' . print_r($_FILES,true) . '</pre>'; } ?> <html> <head> <title>Quick Test</title> </head> <body> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="att[1]" size="26" /><br> <input type="file" name="att[2]" size="26" /><br> <input type="submit" name="submit" value="testit"> </form> </body> </html> Ken Quote Link to comment https://forums.phpfreaks.com/topic/203213-for-each-loop/#findComment-1064747 Share on other sites More sharing options...
c_pattle Posted May 28, 2010 Author Share Posted May 28, 2010 Thanks. I changed the array to $_FILES['att'] but ummm, I think there are other issues, Warning: fread(): supplied argument is not a valid stream resource in /websites/123reg/LinuxPackage21/co/lo/ur/colourbrush.co.uk/public_html/order_success2.php on line 31 Warning: fclose(): supplied argument is not a valid stream resource in /websites/123reg/LinuxPackage21/co/lo/ur/colourbrush.co.uk/public_html/order_success2.php on line 32 Thank you. Your order was sent successfully Warning: fread(): supplied argument is not a valid stream resource in /websites/123reg/LinuxPackage21/co/lo/ur/colourbrush.co.uk/public_html/order_success2.php on line 31 Warning: fclose(): supplied argument is not a valid stream resource in /websites/123reg/LinuxPackage21/co/lo/ur/colourbrush.co.uk/public_html/order_success2.php on line 32 Thank you. Your order was sent successfully Warning: fread(): supplied argument is not a valid stream resource in /websites/123reg/LinuxPackage21/co/lo/ur/colourbrush.co.uk/public_html/order_success2.php on line 31 Warning: fclose(): supplied argument is not a valid stream resource in /websites/123reg/LinuxPackage21/co/lo/ur/colourbrush.co.uk/public_html/order_success2.php on line 32 Thank you. Your order was sent successfully Warning: fread(): supplied argument is not a valid stream resource in /websites/123reg/LinuxPackage21/co/lo/ur/colourbrush.co.uk/public_html/order_success2.php on line 31 Warning: fclose(): supplied argument is not a valid stream resource in /websites/123reg/LinuxPackage21/co/lo/ur/colourbrush.co.uk/public_html/order_success2.php on line 32 Thank you. Your order was sent successfully Warning: fread(): supplied argument is not a valid stream resource in /websites/123reg/LinuxPackage21/co/lo/ur/colourbrush.co.uk/public_html/order_success2.php on line 31 Warning: fclose(): supplied argument is not a valid stream resource in /websites/123reg/LinuxPackage21/co/lo/ur/colourbrush.co.uk/public_html/order_success2.php on line 32 [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/203213-for-each-loop/#findComment-1064751 Share on other sites More sharing options...
kenrbnsn Posted May 28, 2010 Share Posted May 28, 2010 Did you run my quick script? That will show you what is being returned in the $_FILES array an you can change your script accordingly. Ken Quote Link to comment https://forums.phpfreaks.com/topic/203213-for-each-loop/#findComment-1064761 Share on other sites More sharing options...
c_pattle Posted May 28, 2010 Author Share Posted May 28, 2010 Yeah I did Array ( [att] => Array ( [name] => Array ( [0] => test.txt [1] => test2.txt ) [type] => Array ( [0] => text/plain [1] => text/plain ) [tmp_name] => Array ( [0] => /tmp/php068SwM [1] => /tmp/phpw4TRt1 ) [error] => Array ( [0] => 0 [1] => 0 ) => Array ( [0] => 18 [1] => 35 ) ) ) Everything seems fine. I'm not sure what's wrong. Quote Link to comment https://forums.phpfreaks.com/topic/203213-for-each-loop/#findComment-1064783 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.