又一个查询移动文件上传问题

时间:2014-06-16 20:42:04

标签: php forms email jquery-mobile attachment

首先,我在类似的问题上看到了很多建议,但是,我没有找到任何适合我正在尝试做的事情。

我有一个发布到PHP电子邮件提交的表单,在表单的最后,我希望用户可以选择从相机应用程序或图库附加多张照片(我已经读过那些iPhone不允许访问相机应用程序,但Androids确实如此)并将其全部发送到一个漂亮的小电子邮件中。我一直在我的所有附件行上得到一个未定义的索引错误。现在,我已经用尽了我的copy-fu故障排除,现在已经开始向那些真正了解PHP语法的人提出要求。这是我的表单的末尾,其中附件部分是(整个页面非常广泛和精细,如果需要我可以显示,但这是让我头疼的唯一部分......):

<form data-ajax="false" method="post" action="mo-mail.php" enctype="multipart/form-data">

(所有其他选项,然后......)

<label for="attachments">Upload Photos:</label>
            <input type="file" name="attachment1" id="attachment1" class="ui-input-text" />
            <input type="file" name="attachment2" id="attachment2" class="ui-input-text" />
            <input type="file" name="attachment3" id="attachment3" class="ui-input-text" />
            <input type="file" name="attachment4" id="attachment4" class="ui-input-text" />
            <input type="file" name="attachment5" id="attachment5" class="ui-input-text" />
            <p>*Please only press the Submit button once. Large sized photos require longer time to upload</p>
<input type="submit" name="submit" value="Submit Request" data-disabled="false">

然后我的PHP页面就完整了:

<?php

$MOfirstname = $_POST['MOfirstname'];
$MOlastname = $_POST['MOlastname'];
$MOcompany = $_POST['MOcompany'];
$MOemail = $_POST['MOemail'];
$MOphone = $_POST['MOphone'];
$MOmodelnumber = $_POST['MOmodelnumber'];
$MOvoltage = $_POST['MOvoltage'];
$MOoperation = $_POST['MOoperation'];
$MOfused = $_POST['MOfused'];
$MOtriptype = $_POST['MOtriptype'];
$MOfusesize = $_POST['MOfusesize'];
$MOquantity = $_POST['MOquantity'];
$MOcomments = $_POST['MOcomments'];
$MOdelivery = $_POST['MOdelivery'];
$MOmailinglist = $_POST['MOmailinglist'];
$MO_Function = implode(", ", $_POST['MO_Function']);
$MOquote = implode(", ", $_POST['MOquote']);
$MOAux = implode(", ", $_POST['MOAux']);
$MOAuxSwitchSize = $_POST['MOAuxSwitchSize'];
$MOAuxShuntTripVoltage = $_POST['MOAuxShuntTripVoltage'];
$MOBellAlarm_OTS_Voltage = $_POST['MOBellAlarm_OTS_Voltage'];
$MOBellAlarm_OTS_Operation = $_POST['MOBellAlarm_OTS_Operation'];

$attachment1 = $field_file = $_POST['attachment1'];
$tmpName1 = $_FILES['attachment1']['tmp_name1'];
$fileType1 = $_FILES['attachment1']['type1'];
$fileName1 = $_FILES['attachment1']['name1'];
$attachment2 = $field_file = $_POST['attachment2'];
$tmpName2 = $_FILES['attachment2']['tmp_name2'];
$fileType2 = $_FILES['attachment2']['type2'];
$fileName2 = $_FILES['attachment2']['name2'];
$attachment3 = $field_file = $_POST['attachment3'];
$tmpName3 = $_FILES['attachment3']['tmp_name3'];
$fileType3 = $_FILES['attachment3']['type3'];
$fileName3 = $_FILES['attachment3']['name3'];
$attachment4 = $field_file = $_POST['attachment4'];
$tmpName4 = $_FILES['attachment4']['tmp_name4'];
$fileType4 = $_FILES['attachment4']['type4'];
$fileName4 = $_FILES['attachment4']['name4'];
$attachment5 = $field_file = $_POST['attachment5'];
$tmpName5 = $_FILES['attachment5']['tmp_name5'];
$fileType5 = $_FILES['attachment5']['type5'];
$fileName5 = $_FILES['attachment5']['name5'];

if(empty($MOfirstname)||empty($MOemail)||empty($MOlastname)||empty($MOcompany)||empty($MOphone)) 
{
    echo "<div style ='font:36px Arial,tahoma,sans-serif;color:#ff0000'>Please click the Back button and fill in all contact information.</div>";
    exit;
}

$to = "myemail@privacy.com";
$email_from = 'myemail@privacy.com';
$email_subject = "New Mobile Submission";
$email_body = "You have received a new query from $MOfirstname $MOlastname at $MOcompany about a $MOmodelnumber. They can be reached at $MOphone or $MOemail.\n".

    "Here is the request:\n \n
    Quantity: $MOquantity
    Model: $MOmodelnumber
    Frame Size: $MOvoltage
    Operation: $MOoperation
    Fused: $MOfused
    Fuse Size: $MOfusesize
    Trip Type: $MOtriptype
    Functions: $MO_Function
    Auxillary Devices: $MOAux
        Auxillary Switch Size: $MOAuxSwitchSize
        Auxillary Shunt Trip Voltage: $MOAuxShuntTripVoltage
        Bell Alarm/OTS Operation = $MOBellAlarm_OTS_Operation
        Bell Alarm/OTS Voltage: $MOBellAlarm_OTS_Voltage
    Additional Info: $MOcomments
    Mailing List: $MOmailinglist


    They would like a quote for the following:
        $MOquote

    With a desired delivery of:
        $MOdelivery";


$headers = "From:myemail@privacy.com \r\n";
$headers .= "Reply-To: $MOemail \r\n"; 
    {
          /* Reading file ('rb' = read binary)  */
          $file1 = fopen($tmpName1,'rb');
          $data1 = fread($file1,filesize($tmpName1));
          fclose($file1);

          $file2 = fopen($tmpName2,'rb');
          $data2 = fread($file2,filesize($tmpName2));
          fclose($file2);

          $file3 = fopen($tmpName3,'rb');
          $data3 = fread($file3,filesize($tmpName3));
          fclose($file3);

          $file4 = fopen($tmpName4,'rb');
          $data4 = fread($file4,filesize($tmpName4));
          fclose($file4);

          $file5 = fopen($tmpName5,'rb');
          $data5 = fread($file5,filesize($tmpName5));
          fclose($file5);

            /* a boundary string */
          $randomVal = md5(time());
          $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";

          /* Header for File Attachment */
          $headers .= "\nMIME-Version: 1.0\n";
          $headers .= "Content-Type: multipart/mixed;\n" ;
          $headers .= " boundary=\"{$mimeBoundary}\"";

          /* Multipart Boundary above message */
          $email_body = "This is a multi-part message in MIME format.\n\n" .
          "--{$mimeBoundary}\n" .
          "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
          "Content-Transfer-Encoding: 7bit\n\n" .
          $email_body . "\n\n";

          /* Encoding file data */
          $data1 = chunk_split(base64_encode($data1));
          $data2 = chunk_split(base64_encode($data2));
          $data3 = chunk_split(base64_encode($data3));
          $data4 = chunk_split(base64_encode($data4));
          $data5 = chunk_split(base64_encode($data5));

          /* Adding attchment-file to message*/
          $email_body .= "--{$mimeBoundary}\n" .
          "Content-Type: {$fileType};\n" .
          " name=\"{$fileName1}\"\n" .
          "Content-Transfer-Encoding: base64\n\n" .
          $data1 . "\n\n" .
          "--{$mimeBoundary}--\n";

          $email_body .= "--{$mimeBoundary}\n" .
          "Content-Type: {$fileType};\n" .
          " name=\"{$fileName2}\"\n" .
          "Content-Transfer-Encoding: base64\n\n" .
          $data2 . "\n\n" .
          "--{$mimeBoundary}--\n";

          $email_body .= "--{$mimeBoundary}\n" .
          "Content-Type: {$fileType};\n" .
          " name=\"{$fileName3}\"\n" .
          "Content-Transfer-Encoding: base64\n\n" .
          $data3 . "\n\n" .
          "--{$mimeBoundary}--\n";

          $email_body .= "--{$mimeBoundary}\n" .
          "Content-Type: {$fileType};\n" .
          " name=\"{$fileName4}\"\n" .
          "Content-Transfer-Encoding: base64\n\n" .
          $data4 . "\n\n" .
          "--{$mimeBoundary}--\n";

          $email_body .= "--{$mimeBoundary}\n" .
          "Content-Type: {$fileType};\n" .
          " name=\"{$fileName5}\"\n" .
          "Content-Transfer-Encoding: base64\n\n" .
          $data5 . "\n\n" .
          "--{$mimeBoundary}--\n";
    }
function IsInjected($str)
{
    $injections = array('(\n+)',
           '(\r+)',
           '(\t+)',
           '(%0A+)',
           '(%0D+)',
           '(%08+)',
           '(%09+)'
           );

    $inject = join('|', $injections);
    $inject = "/$inject/i";

    if(preg_match($inject,$str))
    {
      return true;
    }
    else
    {
      return false;
    }
}

if(IsInjected($MOemail))
{
    echo "Bad email value!";
    exit;
}
@mail($to,$email_subject,$email_body,$headers);
header("Location: thank-you.html");

?> 

好的...最终,我希望将附件添加为电子邮件的附件,但我不希望用户有附加所有5张照片......

......我确信有一种比我接近它更简单的方法来实现这一点,但我愿意接受建议......

1 个答案:

答案 0 :(得分:0)

好吧,我觉得缺乏回应是一个迹象,表明我接近这个问题的方式过于复杂,而且/或没有任何意义......因此,没有人愿意接触它。

我能够让它与PHPMailer一起使用。