Jump to content

problem with foreach


cavemong

Recommended Posts

I'm currently working on a page that has the user fill out a form, which then sends form info to an email address with attachments.  I have it set so that the user must attach at least one file to send the form, there are 5 file upload slots.  My problem is that the foreach reports back every upload slot that isn't filled.  For example, if I upload one file and submit the form, it reports back that I have successfully sent the form, but then it also includes the error message for the other 4 slots that I didn't fill.  I simply want it to recognize that at least one file is sent and then negate the other slots that aren't filled.

 

 

   function sendmail() {
   
   if ($_SERVER["REQUEST_METHOD"]=="POST"){
      // now we'll process our uploaded files
   foreach($_FILES as $userfile){
      // store the file information to variables for easier access
      $tmp_name = $userfile['tmp_name'];
      $type = $userfile['type'];
      $name = $userfile['name'];
      $size = $userfile['size'];
  
      // if the upload succeded, the file will exist
      if (file_exists($tmp_name)){email();}
  else {nofile();}
  }
  }   
  }

Link to comment
https://forums.phpfreaks.com/topic/138742-problem-with-foreach/
Share on other sites

Can you insert something like

 

<?php

   function sendmail() {
   
   if ($_SERVER["REQUEST_METHOD"]=="POST"){
      // now we'll process our uploaded files
   foreach($_FILES as $userfile){
      // store the file information to variables for easier access
      if(!empty($userfile))
      {
        $tmp_name = $userfile['tmp_name'];
        $type = $userfile['type'];
        $name = $userfile['name'];
        $size = $userfile['size'];
       }
      // if the upload succeded, the file will exist
      if (file_exists($tmp_name)){email();}
     else {nofile();}
     }
     }   
     }

?>

Link to comment
https://forums.phpfreaks.com/topic/138742-problem-with-foreach/#findComment-725384
Share on other sites

That didn't seem to work.

 

I tried this and it stopped the error messages from appearing when at least one file was attached, but when I attached none it didn't send me to my error function, just a blank page.

 

<?php
   function sendmail() {
   
   if ($_SERVER["REQUEST_METHOD"]=="POST"){
      // now we'll process our uploaded files
   foreach($_FILES as $userfile){
      // store the file information to variables for easier access
      $tmp_name = $userfile['tmp_name'];
      $type = $userfile['type'];
      $name = $userfile['name'];
      $size = $userfile['size'];
  
      // if the upload succeded, the file will exist
      if (file_exists($tmp_name)){email();}
  else if (empty($userfile)){nofile();}
  }
  }   
  } 
?>

Link to comment
https://forums.phpfreaks.com/topic/138742-problem-with-foreach/#findComment-725391
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.