提交后的PHP附件表单获取空白页

提交后的PHP附件表单获取空白页,php,forms,attachment,Php,Forms,Attachment,在提交我的表格后,我收到了邮件,但它将进入黑白屏幕,显示消息发送消息 我尝试在提交表单后重新加载页面,但出现错误 <?php if ($_SERVER['REQUEST_METHOD'] == "POST") { $to = "name@gmail.com"; $subject = "E-mail with attachment"; $from = stripslashes($_POST['fromname']) . "<" . stripslashes($_POST[

在提交我的表格后,我收到了邮件,但它将进入黑白屏幕,显示消息发送消息

我尝试在提交表单后重新加载页面,但出现错误

<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {

  $to = "name@gmail.com";
  $subject = "E-mail with attachment";
  $from = stripslashes($_POST['fromname']) . "<" . stripslashes($_POST['fromemail']) . ">" . "<" . stripslashes($_POST['designation']) . ">";

  // generate a random string to be used as the boundary marker
  $mime_boundary = "==Multipart_Boundary_x" . md5(mt_rand()) . "x";

  // now we'll build the message headers
  $headers = "From: $from\r\n" .
      "MIME-Version: 1.0\r\n" .
      "Content-Type: multipart/mixed;\r\n" .
      " boundary=\"{$mime_boundary}\"";

  $message = "Canditade Resume";
  // when we use it
  $message = "This is a multi-part message in MIME format.\n\n" .
      "--{$mime_boundary}\n" .
      "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
      "Content-Transfer-Encoding: 7bit\n\n" .
      $message . "\n\n";

  // iterating each File type
  print_r($_FILES);

  foreach ($_FILES as $userfile) {

    $tmp_name = $userfile['tmp_name'];
    $type = $userfile['type'];
    $name = $userfile['name'];
    $size = $userfile['size'];


    if (file_exists($tmp_name)) {
      if (is_uploaded_file($tmp_name)) {
        $file = fopen($tmp_name, 'rb');
        $data = fread($file, filesize($tmp_name));
        fclose($file);
        $data = chunk_split(base64_encode($data));
      }
      $message .= "--{$mime_boundary}\n" .
          "Content-Type: {$type};\n" .
          " name=\"{$name}\"\n" .
          "Content-Disposition: attachment;\n" .
          " filename=\"{$tmp_name}\"\n" .
          "Content-Transfer-Encoding: base64\n\n" .
          $data . "\n\n";
    }
  }
  // here's our closing mime boundary that indicates the last of the message
  $message.="--{$mime_boundary}--\n";
  // now we just send the message
  if (@mail($to, $subject, $message, $headers))
    echo "Message Sent";
  else
    echo "Failed to send";
} else {
  ?>
  <form action="index.php" method="post"    enctype="multipart/form-data" name="form1">
      <label>Name</label>
      <input type="text" name="fromname" class="input-block-level" style="width: 100%" required placeholder="Your First Name">
      <label>Email Address</label>
      <input type="text" style="width: 100%" class="input-block-level" required placeholder="Your email address" name="fromemail">
      <label>Designation</label>
      <input type="text" style="width: 100%" class="input-block-level" name="designation" required placeholder="Designation">
      <label>Upload Your CV</label>
      <input type="file" class="input-block-level" required placeholder="Upload Your CV" name="file1">
      </div>
      <input type="submit" name="Submit" value="Submit" class="btn btn-primary btn-large pull-left" onclick="javascript: form.action='index.php';">
  </form>
<?php } ?>

名称
电子邮件地址
任命
上传你的简历

请帮助我

标题位置
中指定要重定向到的页面名称

改变

if (@mail($to, $subject, $message, $headers))
    echo "Message Sent";
  else
    echo "Failed to send";
} else {

yourpage.php

<?php
if(isset($_GET['message'])){
  echo $_GET['message'];
}
.
.

?>

您的编码有点混乱。以下是我所做的调整:

我还注释掉了print_r($_文件),因为这将显示用户按submit时不希望显示的已传递内容的数组

因此,我重命名了$u帖子,并将其设置为isset:

<?php
if (isset($_POST['send'])) {

    $to = "name@gmail.com";
    $subject = "E-mail with attachment";
    $from = stripslashes($_POST['fromname']) . "<" . stripslashes($_POST['fromemail']) . ">" . "<" . stripslashes($_POST['designation']) . ">";

    // generate a random string to be used as the boundary marker
    $mime_boundary = "==Multipart_Boundary_x" . md5(mt_rand()) . "x";

    // now we'll build the message headers
    $headers = "From: $from\r\n" .
        "MIME-Version: 1.0\r\n" .
        "Content-Type: multipart/mixed;\r\n" .
        " boundary=\"{$mime_boundary}\"";

    $message = "Canditade Resume";
    // when we use it
    $message = "This is a multi-part message in MIME format.\n\n" .
        "--{$mime_boundary}\n" .
        "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
        "Content-Transfer-Encoding: 7bit\n\n" .
        $message . "\n\n";

    // iterating each File type
    //print_r($_FILES);

    foreach ($_FILES as $userfile) {

        $tmp_name = $userfile['tmp_name'];
        $type = $userfile['type'];
        $name = $userfile['name'];
        $size = $userfile['size'];


        if (file_exists($tmp_name)) {
            if (is_uploaded_file($tmp_name)) {
                $file = fopen($tmp_name, 'rb');
                $data = fread($file, filesize($tmp_name));
                fclose($file);
                $data = chunk_split(base64_encode($data));
            }
            $message .= "--{$mime_boundary}\n" .
                "Content-Type: {$type};\n" .
                " name=\"{$name}\"\n" .
                "Content-Disposition: attachment;\n" .
                " filename=\"{$tmp_name}\"\n" .
                "Content-Transfer-Encoding: base64\n\n" .
                $data . "\n\n";
        }
    }
    // here's our closing mime boundary that indicates the last of the message
    $message .= "--{$mime_boundary}--\n";
    // now we just send the message
    if (@mail($to, $subject, $message, $headers)) {
        echo "Message Sent";
    } else {
        echo "Failed to send";
    }
}
    ?>

不确定为什么要将PHP包装在html端,但这就是为什么没有将您带回表单端,而只是一个空白页面

 <form action="" method="post"    enctype="multipart/form-data" name="form1">
        <label>Name</label>
        <input type="text" name="fromname" class="input-block-level" style="width: 100%" required placeholder="Your First Name">
        <label>Email Address</label>
        <input type="text" style="width: 100%" class="input-block-level" required placeholder="Your email address" name="fromemail">
        <label>Designation</label>
        <input type="text" style="width: 100%" class="input-block-level" name="designation" required placeholder="Designation">
        <label>Upload Your CV</label>
        <input type="file" class="input-block-level" required placeholder="Upload Your CV" name="file1">
        </div>
        <input type="submit" name="send" value="Submit" class="btn btn-primary btn-large pull-left" onclick="javascript: form.action='index.php';">
    </form>

名称
电子邮件地址
任命
上传你的简历

我已经测试了这段代码,它重定向回表单,表单上方带有“Message sent”(消息已发送)(正如您在任何联系人表单上看到的那样)。

您想实现什么?什么是您不希望出现的错误?表单工作正常,提交后我想验证表单,然后想重新访问其他页面请使用您现在拥有的代码更新问题中的代码。仍然获得此错误数组([file1]=>Array([name]=>sendemail.php[type]=>application/octet stream[tmp\U name]=>/tmp/phphNCMKH)[error]=>0[size]=>801])这不是错误,这是您的print_r语句
print_r($_文件);
!删除该行,它就会工作。我删除的print_r仅在获得白色屏幕后,我想重定向其他页面如果(!empty($var)){//do something}因为表单必填字段很容易被忽略。感谢您的帮助,现在可以重新搜索,但附件文件无法发送到邮件中
<?php
if (isset($_POST['send'])) {

    $to = "name@gmail.com";
    $subject = "E-mail with attachment";
    $from = stripslashes($_POST['fromname']) . "<" . stripslashes($_POST['fromemail']) . ">" . "<" . stripslashes($_POST['designation']) . ">";

    // generate a random string to be used as the boundary marker
    $mime_boundary = "==Multipart_Boundary_x" . md5(mt_rand()) . "x";

    // now we'll build the message headers
    $headers = "From: $from\r\n" .
        "MIME-Version: 1.0\r\n" .
        "Content-Type: multipart/mixed;\r\n" .
        " boundary=\"{$mime_boundary}\"";

    $message = "Canditade Resume";
    // when we use it
    $message = "This is a multi-part message in MIME format.\n\n" .
        "--{$mime_boundary}\n" .
        "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
        "Content-Transfer-Encoding: 7bit\n\n" .
        $message . "\n\n";

    // iterating each File type
    //print_r($_FILES);

    foreach ($_FILES as $userfile) {

        $tmp_name = $userfile['tmp_name'];
        $type = $userfile['type'];
        $name = $userfile['name'];
        $size = $userfile['size'];


        if (file_exists($tmp_name)) {
            if (is_uploaded_file($tmp_name)) {
                $file = fopen($tmp_name, 'rb');
                $data = fread($file, filesize($tmp_name));
                fclose($file);
                $data = chunk_split(base64_encode($data));
            }
            $message .= "--{$mime_boundary}\n" .
                "Content-Type: {$type};\n" .
                " name=\"{$name}\"\n" .
                "Content-Disposition: attachment;\n" .
                " filename=\"{$tmp_name}\"\n" .
                "Content-Transfer-Encoding: base64\n\n" .
                $data . "\n\n";
        }
    }
    // here's our closing mime boundary that indicates the last of the message
    $message .= "--{$mime_boundary}--\n";
    // now we just send the message
    if (@mail($to, $subject, $message, $headers)) {
        echo "Message Sent";
    } else {
        echo "Failed to send";
    }
}
    ?>
 <form action="" method="post"    enctype="multipart/form-data" name="form1">
        <label>Name</label>
        <input type="text" name="fromname" class="input-block-level" style="width: 100%" required placeholder="Your First Name">
        <label>Email Address</label>
        <input type="text" style="width: 100%" class="input-block-level" required placeholder="Your email address" name="fromemail">
        <label>Designation</label>
        <input type="text" style="width: 100%" class="input-block-level" name="designation" required placeholder="Designation">
        <label>Upload Your CV</label>
        <input type="file" class="input-block-level" required placeholder="Upload Your CV" name="file1">
        </div>
        <input type="submit" name="send" value="Submit" class="btn btn-primary btn-large pull-left" onclick="javascript: form.action='index.php';">
    </form>