0

我正在使用 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 ' &nbsp;</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 ' &nbsp; <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'>";
}



?>
4

1 回答 1

0

你应该move_uploaded_file首先,在你使用它之前。

于 2013-04-02T14:37:56.203 回答