我正在使用php邮件表单,但我在使用表单的文件附件时遇到问题。我搜索这个论坛并尝试了一些东西,但它们没有用,可能是因为我的代码有点不同。我的表单正在做的是连接到数据库并提取作业信息(标题城市状态),以便用户不必填写该信息(他们通常会以某种方式弄错)。有人可以看下面的代码,看看我做错了什么。没有错误,邮件通过,它只是没有附加。提前谢谢。
$con = mysql_connect("connection")
$id = $_GET['id'];
if ( isset( $_POST["submit"] )) {
if( $_POST || $_FILES )
//if "email" is filled out, send email
{
//send email
$subject = $_POST['subject'] ;
$title = $_POST['title'] ;
$firstname = $_POST['firstname'] ;
$middleinitial = $_POST['middleinitial'] ;
$lastname = $_POST['lastname'] ;
$email = $_POST['email'] ;
$subject = $_POST['subject'] ;
$city = $_POST['city'] ;
$state = $_POST['state'] ;
$phone = $_POST['phone'] ;
$resume = $_POST['attachment'] ;
$message = "
Position Info
--------------------------------
Title: $title
Location: $city $state
Applicant Info
--------------------------------
First Name: $firstname
Middle Initial: $middleinitial
Last Name: $lastname
E-mail: $email
Phone: $phone";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n"."Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
fixFilesArray($_FILES['attachment']);
foreach ($_FILES['attachment'] as $position => $file)
{
// should output array with indices name, type, tmp_name, error, size
$message .= "--{$mime_boundary}\n";
$fp = @fopen($file['tmp_name'],"rb");
$data = @fread($fp,filesize($file['tmp_name']));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".$file['name']."\"\n"."Content-Description: ".$file['name']."\n" ."Content-Disposition: attachment;\n" . " filename=\"".$file['name']."\";size=".$file['size'].";\n"."Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $from;
$ok = @mail($to, $subject, $message, $headers, $returnpath);
if($ok){ return 1; } else { return 0; }
}
function fixFilesArray(&$files)
{
$names = array( 'name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1);
foreach ($files as $key => $part) {
// only deal with valid keys and multiple files
$key = (string) $key;
if (isset($names[$key]) && is_array($part)) {
foreach ($part as $position => $value) {
$files[$position][$key] = $value;
}
// remove old key reference
unset($files[$key]);
}
}
}
}
$sql = "select id,title,city,state from employment where id = $id";
$res = mysql_query( $sql ) or die( 'Could not display records' );
if ( mysql_num_rows( $res ) > 0 ) {
echo '<form method="post" enctype="multipart/form-data" action="http://careers-admin.dbiservices.com/index.php?content=appform">';
echo '<input type="hidden" name="subject" value="Job Application">';
while ( $row = mysql_fetch_assoc( $res ) ) {
echo '<table>';
echo '<tr><td align="right">';
echo ' <b>Position Applying For</b></td><td> <input type="text" name="title" value="' . $row["title"] . '" size="30"> <br />' . "\n";
echo '<tr><td align="right">';
echo ' <b>Location</b></td><td> <input type="text" name="city" value="' . $row["city"] . ' ' . $row["state"] .'" size="30"> <br />' . "\n";
echo '<tr><td align="right">';
echo ' <b>First Name</b></td><td> <input type="text" name="firstname" size="30"> <br />' . "\n";
echo '<tr><td align="right">';
echo ' <b>Middle Initial</b></td><td> <input type="text" name="middleinitial" size="5"> <br />' . "\n";
echo '<tr><td align="right">';
echo ' <b>Last Name</b></td><td> <input type="text" name="lastname" size="30"> <br />' . "\n";
echo '<tr><td align="right">';
echo ' <b>Email</b></td><td> <input type="text" name="email" size="30"> <br />' . "\n";
echo '<tr><td align="right">';
echo ' <b>Phone</b></td><td> <input type="text" name="phone" size="30"> <br />' . "\n";
echo '<tr><td align="right">';
echo ' </td><td><em>In order to be considered for any open position, you<br> must attach a resume or type in a work history.</em> <br />' . "\n";
echo '<tr><td align="right">';
echo ' <b>Resume</b></td><td> <input type="file" name="attachment[]" size="20" /> <br />' . "\n";
echo '</td></tr></table>';
}
echo ' <input type="submit" name="submit" value="Submit Application">';
echo '</form>';
echo "<br><br><input type=button onClick=\"location.href='index.php'\" value='Back to Jobs'>";
}
?>
答案 0 :(得分:0)
在使用之前,您应先move_uploaded_file
。