Jump to content

Array not working


c_pattle

Recommended Posts

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);

Link to comment
https://forums.phpfreaks.com/topic/203270-array-not-working/
Share on other sites

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/203270-array-not-working/#findComment-1064991
Share on other sites

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>';
}

Link to comment
https://forums.phpfreaks.com/topic/203270-array-not-working/#findComment-1064995
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.