Jump to content

Recommended Posts

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. 

Link to comment
https://forums.phpfreaks.com/topic/203213-for-each-loop/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/203213-for-each-loop/#findComment-1064747
Share on other sites

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]

Link to comment
https://forums.phpfreaks.com/topic/203213-for-each-loop/#findComment-1064751
Share on other sites

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. 

Link to comment
https://forums.phpfreaks.com/topic/203213-for-each-loop/#findComment-1064783
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.