0

我创建了一个简单的 HTML 表单,我尝试在提交表单后使用 PEAR 发送多个附件。说到 PHP,我是个菜鸟,所以我有点不知所措。每次我提交表单时,我只会得到两个附件之一(letter_uploaded_file)。任何帮助表示赞赏。

    <?php 
// Pear library includes
// You should have the pear lib installed
include('PEAR/Mail.php');
include('PEAR/Mail/mime.php');

//Settings 
$max_allowed_file_size = 4096; // size in KB 
$allowed_extensions = array("pdf", "txt", "doc", "docx");
$upload_folder = './uploads/'; //<-- this folder must be writeable by the script
$your_email = 'gradysapp@gmail.com';//<<--  update this to your email address

$errors ='';

if(isset($_POST['submit']))
{
//Get the uploaded file information
$names_of_files = array();
$names_of_files[] = basename($_FILES["resume_uploaded_file"]['name']);
$names_of_files[] = basename($_FILES["letter_uploaded_file"]['name']);

//get the file extension of the file
$type_of_uploaded_file = array();
$type_of_uploaded_file[] = basename($_FILES['resume_uploaded_file']['type']);
$type_of_uploaded_file[] = basename($_FILES['letter_uploaded_file']['type']);

$size_of_uploaded_file = array();
$size_of_uploaded_file[] = basename($_FILES["resume_uploaded_file"]["size"]/2048);
$size_of_uploaded_file[] = basename($_FILES["letter_uploaded_file"]["size"]/2048);

///------------Do Validations-------------
if(empty($_POST['name'])||empty($_POST['email']))
{
$errors .= "\n Name and Email are required fields. ";   
}
if(IsInjected($visitor_email))
{
$errors .= "\n Bad email value!";
}

if($size_of_uploaded_file > $max_allowed_file_size ) 
{
$errors .= "\n Size of file should be less than $max_allowed_file_size";
}

//------ Validate the file extension -----
$allowed_ext = false;
for($i=0; $i<sizeof($allowed_extensions); $i++) 
{ 
    if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
{
    $allowed_ext = true;        
}
}

if(!$allowed_ext)
{
$errors .= "\n The uploaded file is not supported file type. ".
" Only the following file types are supported: ".implode(',',$allowed_extensions);
}

//send the email 
if(empty($errors))
{
    //copy the temp. uploaded file to uploads folder
    $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
    $tmp_path = array();
    $tmp_path[] = basename($_FILES["resume_uploaded_file"]["tmp_name"]);
    $tmp_path[] = basename($_FILES["letter_uploaded_file"]["tmp_name"]);

    if(is_uploaded_file($tmp_path))
    {
    if(!move_uploaded_file($tmp_path,$path_of_uploaded_file))
    {
        $errors .= '\n error while moving the uploaded file';
    }
}

//send the email
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$phone = $_POST["phone"];
$position = $_POST["position"];
$to = $your_email;
$subject="New Job Applicant Submission";
$from = $your_email;
$text = "A user  $name has sent you this message:\n $user_message";
$text .= "Phone: " . $phone . "\n";
$text .= "Email: " . $visitor_email . "\n";
$text .= "Position: " . $position . "\n";

$message = new Mail_mime();
$message->setTXTBody($text);
$message->addAttachment($path_of_uploaded_file);
$message->addAttachment($path_of_uploaded_file);
$body = $message->get();
$extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email);
$headers = $message->headers($extraheaders);
$mail = Mail::factory("mail");
$mail->send($to, $headers, $body);
    //redirect to 'thank-you page
    header('Location: careers_thank-you.html');
}
}
4

2 回答 2

1

好吧,一方面,您只调用一次 addAttachment :

$message->addAttachment($path_of_uploaded_file);

您需要为每个上传的文件执行一次。

另外,我建议您使用 move_uploaded_file() 而不是 copy() 将上传的文件放置在它们最终的位置。

于 2012-08-15T21:09:51.310 回答
1

您只得到一个文件,因为您使用相同的变量来处理这两个文件:

$name_of_uploaded_file =  basename($_FILES['resume_uploaded_file']['name']);
$name_of_uploaded_file =  basename($_FILES['letter_uploaded_file']['name']);

这应该是这样的:

$names_of_files = array();
$names_of_files[] = basename($_FILES['resume_uploaded_file']['name']);
$names_of_files[] = basename($_FILES['letter_uploaded_file']['name']);

您必须在其余代码中传播此模式,并在附加文件时循环文件。

于 2012-08-15T21:12:09.033 回答