2011-05-17 93 views
1
<?php 

require_once 'swift-mailer/lib/swift_required.php'; 

if (isset($_POST['submit'])) { 

//File upload 

// Where the file is going to be placed 
$target_path = "uploads/"; 

// Add the original filename to our target path. 
//Result is "uploads/filename.extension" 
$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 

if (is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) { 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['uploadedfile']['name']). 
    " has been uploaded"; 
} else{ 
    echo "There was an error uploading the file, please try again!"; 
} 
} 
//End of file upload 

//Create the Transport 
$transport = Swift_MailTransport::newInstance(); 

//Create the Mailer using your created Transport 
$mailer = Swift_Mailer::newInstance($transport); 

//Create the message 
$message = Swift_Message::newInstance(); 

    //Give the message a subject 
$message->setSubject('New data submitted'); 

$ip=$_SERVER['REMOTE_ADDR']; 
$date=date("l, F j, Y, g:i a"); 

    //Give it a body 
$message->setBody("Here is the information submitted to 
your site 
from $ip on $date.\n\n 
--------------------------------\n\n 
name: User \n\n 
email address: [email protected] \n\n 
subject: Test \n\n 
comment: Test comment"); 

    //Add alternative parts with addPart() 
//$message->addPart('My amazing body in plain text', 'text/plain'); 

//Create the attachment 
// * Note that you can technically leave the content-type parameter out 
$attachment = Swift_Attachment::fromPath($target_path); 

//Attach it to the message 
$message->attach($attachment); 

//Using setTo() to set all recipients in one go 
$message->setTo(array('[email protected]' => 'VB')); 

//Set a From: address including a name 
$message->setFrom(array('[email protected]' => 'Contact Us')); 

//Send the message 
$numSent = $mailer->send($message); 

//printf("Sent %d messages\n", $numSent); 

if(isset($_FILES['uploadedfile'])){ 
$pathfile = "/home/me/mysite/uploads/" . $_FILES['uploadedfile']['name']; 
@unlink($pathfile); 
} 

$to = "$email"; 
$subject = "Thank You!"; 
$body = "Thank you for e-mailing us. We will reply as soon as possible."; 
// fix for thank you email address 
$headers = "From: [email protected]\r\n" . 
      "Reply-To: [email protected]\r\n" . 
      "X-Mailer: PHP/" . phpversion(); 
     mail($to, $subject, $body, $headers); 






    } 



?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
    <head> 
     <title>Plain Form</title> 
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 

    </head> 
    <body> 

<form id="form" action="plain_upload.php" method="post"> 
    <p> 

     <input type="file" name="uploadedfile" /> 
    </p> 

    <p> 
     <input type="submit" name="submit" /> 
    </p> 

</form> 

    </body> 
</html> 

謝謝大家提前!爲什麼文件不能通過這個HTML/PHP上傳?

+0

打開錯誤報告,以便您可以告訴我們當您嘗試時會發生什麼。附註:希望我可以編輯修復格式。可惜。 – JClaspill 2011-05-17 19:19:30

回答

0

你需要一個enctype添加到您的表單標籤

<form method="post" action="plain_upload.php" enctype="multipart/form-data"> 
+0

哦,謝謝你! 我怎麼能忽略這個? :) – vlevsha 2011-05-17 19:50:17

1

表單標籤需要有enctype="multipart/form-data"才能使文件上傳正常工作。

0

您需要的enctype屬性添加到您的表單文件上傳。

<form method = 'post' action = 'action' enctype = 'multipart/form-data'> 
... 
</form> 
+0

非常感謝! – vlevsha 2011-05-17 19:50:35

+0

沒問題。有時很容易忽略簡單的事情:) – 2011-05-17 19:58:10

相關問題