如何在图像上传php表单中添加电子邮件评论

时间:2014-04-17 13:40:02

标签: php image forms email

我目前正在构建一个php上传页面,允许用户上传3张图片并上传详细信息。我设法让图片上传工作了,但是有人可以告诉我如何在价格,颜色,尺寸,年龄和价格上增加5个表单部分。当他们上传到我的服务器上的文件夹时,可以通过电子邮件发送给我的描述?谢谢你我的编码到目前为止。

<?php

// filename: upload.form.php

// first let's set some variables

// make a note of the current working directory relative to root.
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);

// make a note of the location of the upload handler
$uploadHandler = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'multiple.upload.processor.php';

// set a max file size for the html upload form
$max_file_size = 30000; // size in bytes

// now echo the html page
?>


<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">

    <link rel="stylesheet" type="text/css" href="stylesheet.css">

    <title>Upload form</title>

</head>

<body id="body">

<form id="Upload" action="<?php echo $uploadHandler ?>" enctype="multipart/form-data" method="post">

    <h1>
        Sell-A-Car
    </h1>

    <p>
        <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>">
    </p>

    <p>
        <label for="file1">Item Image 1:</label>
        <input id="file1" type="file" name="file[]">
    </p>

    <p>
        <label for="file2">Item image 2:</label>
         <input id="file2" type="file" name="file[]">
    </p>

    <p>
        <label for="file3">Item image 3:</label>
        <input id="file3" type="file" name="file[]">
    </p>

    <p>
        <label for="submit">Press to...</label>
        <input id="submit" type="submit" name="submit" value="SELL IT!">
    </p>

</form>


</body>

</html>

第二个php文件是

<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 100000) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>

2 个答案:

答案 0 :(得分:0)

表单上已经有正确的enctype,您只需要在表单中添加所需的文本或其他任何输入。例如:

    <input type="text" name="myTextField"/>

然后在您的PHP中,您可以像这样访问它:

    $myTextFieldValue = $_POST['myTextField'];

答案 1 :(得分:0)

如果您要附加图片,我建议您查看PHPMailer。它内置了附件功能。因此,在您的第二页上,您的代码将类似于:

    include("class.phpmailer.php");
    include("class.smtp.php");

    $mail = new PHPMailer;
    $mail->IsSMTP();
    $mail->Host = 'yourmailserver';
    $mail->Username = 'user';
    $mail->Password = 'pw';
    $mail->Timeout = 60;

    $mail->From = 'no-reply@yourdomain.com';

    $mail->AddAddress($data['email']);  

    foreach($_FILES["file"] as $f){
        $mail->AddAttachment($f['tmp_name']);
    }

    $mail->Subject = "Subject";
    $mail->Body = "Price" . $_POST['price']"\r\n";
    $mail->Body .= "Color" . $_POST['color']"\r\n";
        $mail->Body .= "Size" . $_POST['size']"\r\n";


   if(!$mail->Send()) {  
        $mail->SMTPDebug = true;
        $mail->Debugoutput = 'echo';
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
        exit;
    }   

并确保在第一页html中添加参数:

<p>
        <label for="color">Color:</label>
        <input id="color" type="text" name="color">
    </p>