0

我正在上传一个文件,然后将其作为附件发送给我自己。我是 php 新手,我尝试在网上查找内容并编写了这段代码。

 <?php
    function mail_attachment($files, $path, $mailto, $subject, $message) {

$uid = md5(uniqid(time()));
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";

foreach ($files as $filename) { 

    $file = $path.$filename;
    $name = basename($file);
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));

    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
}

$header .= "--".$uid."--";
if (@mail($mailto, $subject, $message, $header)) {
    echo "mail send ... OK";
} else {
    echo "mail send ... ERROR!";
}
}



   //Enter your email address here
  $mailto="abc@mail.com";
  $subject="Form Details";

   $FirstName = $_POST['firstName'] ;
   $LastName = $_POST['lastName'] ;
$EmailAdress= $_POST['emailAddress'] ;
   $ContactNumber= $_POST['contactNumber'] ;
   $ApartmentType= $_POST['apartmentType'] ;
   $ApartmentLocation = $_POST['apartmentLocation'] ;
   $CheckIn = $_POST['checkIn'] ;
   $CheckOut = $_POST['checkOut'] ;
   $NumberOfAdults = $_POST['numberOfAdults'] ;
   $NumberOfChildren = $_POST['numberOfChildren'] ;
   $TermsAndConditions =$_POST['termsAndConditions'];

   $required = array('firstName','lastName','emailAddress','contactNumber','apartmentType','apartmentLocation','checkIn','checkOut','numberOfAdults','numberOfChildren');

   $error = false;

   foreach($required as $field) {
     if (empty($_POST[$field])) {
       $error = true;
     }
   }

   if ($error) {
     echo "All fields are required.";
     exit;
   } 

   if ( $TermsAndConditions=='disagree') {
     echo "Please agree to the terms and conditions";
     exit;
   }

    $message="First Name:\t $FirstName \n\n" . 
"Last Name:\t $LastName\n\n". 
"Email Address:\t $EmailAddress \n\n". 
"Contact Number:\t $ContactNumber \n\n". 
"Apartment Types:\t $ApartmentType \n\n". 
"Apartment Location:\t $ApartmentLocation \n\n" .
"Check in:\t $CheckIn \n\n" .
"Check out:\t $CheckOut \n\n" .
"Number of Adults:\t $NumberOfAdults \n\n" .
"Number of Children:\t $NumberOfChildren \n\n";

   $uploaddir = './';
   $x=0;
   foreach ($_FILES["documents"]["error"] as $key => $error) 
   {
   if ($error == UPLOAD_ERR_OK) {
    $tmp_name = $_FILES["documents"]["tmp_name"][$key];
    $name = basename($_FILES["pictures"]["name"][$key]);
$files[$x]=$name;
$x++;
    move_uploaded_file($tmp_name, $uploaddir.$name);
       }
   }

   $path = $_SERVER['DOCUMENT_ROOT'];
   mail_attachment($files, $path, $mailto, $subject, $message);


   ?>

我得到两个foreach的错误:

  Warning: Invalid argument supplied for foreach() in /home/a4824849/public_html/PhpFile.php on line 88

 Warning: Invalid argument supplied for foreach() in /home/a4824849/public_html/PhpFile.php on line 12

什么可能是错的?

4

1 回答 1

1

Foreach获取数组并将数组的每个值都赋予varname您定义的 。Invalid Argument 仅仅意味着您没有提供有效的数组。

对于 l.12,它只是意味着您没有将数组作为第一个 arg 传递给您的函数,这是由于您的错误行 88。

我假设您的问题是您尚未尝试上传文件,因此$_FILES未定义。根据我使用$_FILES超全局变量的经验,您通常必须通过enctype multi-part/formdata. 要调试并检查它是否已定义,请var_dump or print_r在循环$_FILES之前执行一次。foreach作为额外的安全措施,您可能希望将所述循环和对 mail_attachment 函数的调用包装在if (isset($_FILES))

希望这可以帮助

于 2013-11-14T17:34:01.207 回答