【问题标题】:How to add emailed comments in image upload php form如何在图片上传 php 表单中添加电子邮件评论
【发布时间】:2014-06-01 19:40:23
【问题描述】:

我目前正在构建一个 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"];
}
?>

【问题讨论】:

    标签: php image forms email


    【解决方案1】:

    如果您想附上图片,我建议您查看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>
    

    【讨论】:

      【解决方案2】:

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

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

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

          $myTextFieldValue = $_POST['myTextField'];
      

      【讨论】:

      • 嗨蒂姆,很抱歉没有第一个例子:)
      • 嗯,是的,感谢@Mack 添加格式。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      相关资源
      最近更新 更多