0

这只是代码的一个示例,基本上我有一个包含 4 个文件的表单要上传。这些文件已成功保存在我的服务器上的指定文件夹中。但是,我显然没有正确连接它们,因为它们是空的……有人可以帮忙吗?

另外,附件显示的是目录名而不是文件名……我错过了什么?

非常感谢!!!

<?php
$username= protect($_POST['username']); 
$firstName= protect($_POST['firstName']); 
$lastName= protect($_POST['lastName']); 
$email= protect($_POST['email']); 
$program= protect($_POST['program']); 
$dob= protect($_POST['dob']); 
$transcript= $_FILES['transcript']; 
$resume= $_FILES['resume']; 
$letterIntent= protect($_POST['letterIntent']); 
$reference1= $_FILES['reference1']; 
$reference2= $_FILES['reference2']; 
$relevantExperience= protect($_POST['relevantExperience']); 
$volunteerExperience= protect($_POST['volunteerExperience']); 
$reasonsAbroad= protect($_POST['reasonsAbroad']); 
$definitionSuccess= protect($_POST['definitionSuccess']); 
$futureGoals= protect($_POST['futureGoals']); 
$challengeYourself= protect($_POST['challengeYourself']); 
$challengeDevelopment= protect($_POST['challengeDevelopment']); 



if (isset($_FILES) && (bool) $_FILES) {
$allowed_ext = array('doc', 'pdf', 'txt', '');
$files = array();

foreach($_FILES as $name=>$file) {
    $file_name = $file['name'];
    $file_tmp = $file['tmp_name'];
    $file_size = $file['size'];
    $path_parts = pathinfo($file_name);
    $file_ext = $path_parts['extension'];

    if(!in_array($file_ext, $allowed_ext)) {
        die ("extension for file $file_name not allowed");
    }

    if($file_size > 2097152) {
        die ("File size must be under 2MB");
    }

    $server_file = "wp-content/uploads/application-documents";
    move_uploaded_file($file_tmp, "$server_file/$file_name");

    array_push($files, $server_file);
}
    $to = ".....@gmail.com";
    $from = "$email"; 
    $subject ="Application from $firstName $lastName with attachments"; 
    $msg = "hello";
    $headers = "From: $from";

    //define email boundaries
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    $headers .= "\nMIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/mixed;\n"; 
    $headers .= " boundary=\"{$mime_boundary}\"";

    $message ="\n\n--{$mime_boundary}\n";
    $message .="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
    $message .="Content-Transfer-Encoding: 7bit\n\n" . $msg . "\n\n"; 
    $message .= "--{$mime_boundary}\n";

foreach($files as $file) {
$aFile = fopen($file,"rb");
$data = fread($aFile,filesize($file));
fclose($aFile);
$data = chunk_split(base64_encode($data));

$message .= "Content-Type: {\"application/octet-stream\"};\n";
$message .= " name=\"$file\"\n";
$message .= "Content-Disposition: attachment;\n";
$message .= " filename=\"$file\"\n";
$message .= "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";

$message .= "--{$mime_boundary}\n";
}
$ok = @mail($to, $subject, $message, $headers); 
    if ($ok) { 
        echo "<p>mail sent to $to</p>"; 
    } else { 
        echo "<p>mail could not be sent!</p>"; 
    } 
}

}
?> 
4

1 回答 1

1

我知道这不是您帖子的答案,但是在过去遇到此类问题后,我放弃并使用了PHPMailer。如果你仍然想自己动手,你应该看看它的源代码。

于 2013-05-23T01:45:20.593 回答