【问题标题】:Website form - Multiple file upload [duplicate]网站表单 - 多个文件上传[重复]
【发布时间】:2014-08-13 18:54:19
【问题描述】:

所以我搜索了很多类似的问题,但似乎找不到解决方案。

我有一个联系表格,我希望它能够发送多张图片。我现在拥有的代码有三个图片输入字段,但现在我只知道如何让其中一张图片上传和发送。我很确定我需要某种循环或数组,但这不是我的强项。

HTML

        <form action="test.php" method="post" autocomplete="on" enctype="multipart/form-data" accept-charset='UTF-8'>
        <fieldset><label for="Name">Name <sup>*</sup></label>
        <input name="name" placeholder="full name" id="Name" required="" autofocus><br>

        <label for="email">E-mail <sup>*</sup></label>
        <input type="email" name="email" placeholder="youremail@domain.com" id="email" required=""><br>

        <label for="phoneNumber">Phone # <sup>*</sup></label>
        <input type="tel" name="phone" placeholder="XXX-XXX-XXXX" id="phoneNumber" required=""><br>
        </fieldset>
        <fieldset>
        <label for="year">Year <sup>*</sup></label>
        <input type="text" name="year" placeholder="year of car" id="year" required=""><br>

        <label for="make">Make <sup>*</sup></label>
        <input type="text" name="make" placeholder="make of car" id="make" required=""><br>

        <label for="model">Model <sup>*</sup></label>
        <input type="text" name="model" placeholder="model of car" id="model" required=""><br>
        </fieldset>
        <label for="State" class="state">State <sup>*</sup></label><br>
        <input type="text" class="state" name="state" id="state" placeholder="Do not enter if you are human">

        <fieldset>
        <legend>Add Photos <sup>*</sup></legend>
        <input type="file" name="attachment" required=""><br>
        <input type="file" name="attachment"><br>
        <input type="file" name="attachment"><br>
        </fieldset>

        <label for="comments">Comments</label><br>
        <textarea  style="float: left;" rows="4" name="comment" id="comments" placeholder="additional comments"></textarea>
        <br>

        <input class="button" type="submit" name="submit" value="Send" class="button">

        <p class="required"><sup>*</sup> denotes a required field.</p>

    </form>

PHP

<?php

$to = '';
$name = $_POST['name'];
$email  =   $_POST ['email'];
$phone = $_POST['phone'];
$year = $_POST['year'];
$make = $_POST['make'];
$model = $_POST['model'];
$comment = $_POST['comment'];
$state = $_POST['state'];
$message = "

Name: $name
E-mail: $email
Phone: $phone
Year: $year
Make: $make
Model: $model
Message: $comment
";

if($_POST['state'] != ''){
echo "It appears you are a bot!";
exit();
}
else{
//process the rest of the form
}

/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];

/* Start of headers */
$headers = "From: $name $email";

if (file($tmpName)) {
/* Reading file ('rb' = read binary)  */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);

/* 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 */
$message = "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" .
$message . "\n\n";

/* Encoding file data */
$data = chunk_split(base64_encode($data));

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


$flgchk = mail ("$to", "$subject", "$message", "$headers");

if($flgchk){
echo "A email has been sent. We will get back to you as soon as
possible.";
}
else{
echo "Error in Email sending";
}
?>

【问题讨论】:

    标签: php forms web


    【解决方案1】:

    您的所有三个字段都被命名为“附件”。给它们起唯一的名称,例如“attachment1”、“attachment2”等。

    然后在您的上传中使用它:

    for ($x = 1; $x <= $numberOfFiles; ++$x) {
        $tmpName = $_FILES['attachment'.$x]['tmp_name']; 
        $fileType = $_FILES['attachment'.$x]['type']; 
        $fileName = $_FILES['attachment'.$x]['name'];
        // do your upload here
    }
    

    您甚至可以在这样的循环中执行此操作,这将允许您在不更改后端代码的情况下添加更多文件字段(可能使用 JavaScript),只要您保持命名模式一致即可。

    $x = 1;
    while (isset($_FILES['attachment'.$x])) {
            $tmpName = $_FILES['attachment'.$x]['tmp_name']; 
            $fileType = $_FILES['attachment'.$x]['type']; 
            $fileName = $_FILES['attachment'.$x]['name'];
            // do your upload here
            ++$x;
    }
    

    这只是在每个周期增加x,并检查是否发送了具有该名称的文件。

    在有人用大文件和/或网络垃圾填满您的收件箱之前,您可能还应该检查文件的大小和类型。


    更新:这对您来说应该是一个最完整的解决方案。我目前无法为您测试它,因此它可能无法直接开箱即用,但它至少应该为您指明正确的方向。在这段代码 sn-p 中,它首先设置电子邮件的初始部分,然后循环并一次添加每个文件。

    <?php
    $to = '';
    $name = $_POST['name'];
    $email  = $_POST ['email'];
    $phone = $_POST['phone']; 
    $year = $_POST['year']; 
    $make = $_POST['make']; 
    $model = $_POST['model']; 
    $comment = $_POST['comment']; 
    $state = $_POST['state']; 
    $message = "Name: $name
    E-mail: $email
    Phone: $phone
    Year: $year
    Make: $make
    Model: $model
    Message: $comment";
    
    if($_POST['state'] != ''){
        echo "It appears you are a bot!";
        exit();
    }
    
    /* Start of headers */ 
    $headers = "From: $name $email"; 
    
    /* 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 */
    $message = "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" . 
    $message . "\n\n";
    
    /* Loop through files */
    $x = 1;
    while (isset($_FILES['attachment'.$x])) {
        /* GET File Variables */ 
        $tmpName = $_FILES['attachment']['tmp_name']; 
        $fileType = $_FILES['attachment']['type']; 
        $fileName = $_FILES['attachment']['name']; 
    
        /* Skip invalid files */
        if (!file($tmpName)) {
            ++$x;
            continue;
        }
    
        /* Reading file ('rb' = read binary)  */
        $file = fopen($tmpName,'rb'); 
        $data = fread($file,filesize($tmpName)); 
        fclose($file); 
    
        /* Encoding file data */
        $data = chunk_split(base64_encode($data)); 
    
        /* Adding attchment-file to message*/
        $message .= "--{$mimeBoundary}\n" . 
        "Content-Type: {$fileType};\n" . 
        " name=\"{$fileName}\"\n" . 
        "Content-Transfer-Encoding: base64\n\n" . 
        $data . "\n\n";
    
        ++$x;
    }
    
    /* Close the message */
    $message .= "--{$mimeBoundary}--\n";
    
    $flgchk = mail ("$to", "$subject", "$message", "$headers"); 
    
    if($flgchk){
        echo "A email has been sent. We will get back to you as soon as possible.";
    } else {
        echo "Error in Email sending";
    }
    ?>
    

    【讨论】:

    • 感谢您的回复!我认为这对我来说很有意义,但现在我的信息甚至没有发送。我会在上传正确之前将此代码放在正确的位置吗?
    • @user3746432 - 关闭,但不完全。在我的每个代码示例中,您需要将 // do your upload here 行替换为您读取文件的过程,然后将其添加为电子邮件的一部分。让我知道这是否有意义;如果没有,我可以为您提供更多代码来更新我的问题。
    • @user3746432 - 我已经为您发布了一个近乎完整的解决方案。它未经测试,但至少应该为您指明正确的方向:)
    • 谢谢!这无疑为我指明了正确的方向。我想我可以从这里得到它。
    • 好东西!让我知道是否有任何错误 - 我可以在答案中更正它们,以便未来的用户可以获得完整的解决方案。
    猜你喜欢
    • 2015-12-04
    • 2013-04-01
    • 2017-05-18
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多