2013-01-20 34 views
0

當我使用這個表單時,它完美地發送文件,除了......它離開了擴展名。我知道我發送的是什麼擴展名,所以我能夠看到文件沒有損壞,但是我的客戶不知道他們得到了什麼擴展名(有5種可能性),並且它是不專業的。我怎樣才能做到這一點,這就給客戶發送了正確的擴展名的文件。正確的名字也會很好,但不是強制性的。PHP表單效果很好,但是不包含文件擴展名

HTML:

</p> 
<title>The Legend Maker - submit a story (test server)</title> 
<link href="/CSS/CSS.css" rel="stylesheet" type="text/css"> 

<div align="center"> 
    <p><span class="linkText"><a href="/index.html">Home</a> <a href="/contact-us.php">Contact Us</a> <a href="/payments.html">Payments</a></span> </p> 
    <p>&nbsp;</p> 
    <h2 class="headingText">&nbsp;</h2> 
    <h2 class="headingText">&nbsp;</h2> 
    <h2 class="headingText">&nbsp;</h2> 
    <h2 class="headingText">(for testing purposes)</h2> 
    <h2 class="headingText">Submit a story test server</h2> 
</div> 
<form method="post" action="scripts/email.php" enctype="multipart/form-data"> 
<table width="476" height="468" border="0" cellpadding="0" cellspacing="2"> 
<tr> 
<td width="23%" height="25" class="bodytext"><p align="center">Your Name:</p></td> 
<td width="77%"><input name="name" type="text" id="name" size="32"></td> 
</tr> 
<tr> 
<td height="25" class="bodytext"><p align="center">Email Address:</p></td> 
<td><input name="email" type="text" id="email" value="[email protected]" size="32"></td> 
</tr> 
<tr> 
<td height="44" class="bodytext"><p align="center">Recipient's Gender:</p></td> 
<td><label> 
    <select name="gender" id="gender"> 
    <option value="male" selected="selected">Male</option> 
    <option value="female">Female</option> 
    <option value="other">Other</option> 
     </select> 
</label></td> 
</tr> 
<tr> 
<td height="44" class="bodytext"><p align="center">Recipient's Name:</p> </td> 
<td align="left" valign="top"><input name="recipientname" type="text" id="recipientname" value="ex: Jonny" size="32" /></td> 
</tr> 
<tr> 
    <td height="44" class="bodytext"><p align="center">Recipient's Interests or Hobbies:</p></td> 
    <td align="left" valign="top"><textarea name="hobbies" cols="50" rows="6" id="hobbies">ex: horseback riding, biking...</textarea></td> 
</tr> 
<tr> 
    <td class="bodytext"><p align="center">Recipient's Age:</p></td> 
    <td align="left" valign="top"><input name="age" type="text" id="age" size="5" maxlength="3" /></td> 
</tr> 
<tr> 
    <td class="bodytext"><p align="center">Other Important Information:</p></td> 
    <td align="left" valign="top"><textarea name="otherinfo" cols="50" rows="7" id="otherinfo">ex: other things the recipient may enjoy or their favorite superhero 
Please specify information you are giving us also: 
don't do this: superman 
submit it like this: favorite superhero: superman</textarea></td> 
</tr> 
<tr> 
    <td class="bodytext"><p align="center">Images You Would Like To Include In Story:</p></td> 
    <td align="left" valign="top"><label> 
    <br /> 
    <input type="file" name="file" id="file" /> 
    </label></td> 
</tr> 
<tr> 
    <td class="bodytext"><p align="center">&nbsp;</p></td> 
    <td align="left" valign="top"><input type="submit" name="Submit" value="Send" /></td> 
</tr> 
</table> 
</form> 

PHP:

<?php 
    require_once '../PHPMailer_5.2.2/class.phpmailer.php'; 

    $name = $_POST['name'] ; 
    $email = $_POST['email'] ; 
    $gender = $_POST['gender'] ; 
    $recipientname = $_POST['recipientname'] ; 
    $hobbies = $_POST['hobbies'] ; 
    $age = $_POST['age'] ; 
    $otherinfo = $_POST['otherinfo'] ; 
    $file = $_POST['file'] ; 
    $body = "Name: $name 
    Email: $email 
    Gender: $gender 
    Recipient's name: $recipientname 
    Hobbies: $hobbies 
    Age: $age 
    Other Information: $otherinfo"; 

    $mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch 

    try { 
     $mail->AddAddress('***[email protected]', 'Michael ***'); 
     $mail->SetFrom($email, $name); 
     $mail->AddReplyTo($email, $name); 
     $mail->Subject = "Message From Legendmaker Customer: $name"; 
     $mail->Body = $body; 
     $mail->AddAttachment($_FILES['file']['tmp_name']);  // attachment 
     $mail->Send(); 
     echo "Story Request Sent Successfully</p>\n"; 
    } catch (phpmailerException $e) { 
     echo $e->errorMessage(); //Pretty error messages from PHPMailer 
    } catch (Exception $e) { 
     echo $e->getMessage(); //Boring error messages from anything else! 
    } 
    ?> 
+0

什麼是您的附件類型? –

+0

您未設置附件的文件名。 '$ _FILES ['file'] ['tmp_name']'將成爲系統中文件的路徑,通常以不包含擴展名的方式命名。 – datasage

+0

你搖滾datasage! –

回答

0

我查PHPMailer的來源,這是我發現的函數簽名。

AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') 

解決你的問題你應該做的是這樣的:

$mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']); 
+0

該類型可以是任何圖像文件。 Png,gif,jpg,bmp等 –

+0

該代碼將從原始上傳的文件中獲取文件名,該文件可能具有文件擴展名。除非您正在檢查文件名並強制執行文件類型,否則它實際上可能是任何內容。 – datasage

相關問題