2011-07-24 66 views
0

我正在尋找一種非常簡單的方法來允許用戶將文件(html)上傳到我的網站。我嘗試了plupload和uploadify之類的東西,他們似乎很難實現/ buggy。那裏有沒有簡單的解決方案?如何輕鬆地讓用戶上傳多個文件?

+1

我發現uploadify易於使用,具體問題是什麼? – 2011-07-24 02:30:56

+0

我想聽聽你的問題,以及我已經使用plu和uplodify,他們看起來很容易,而不是所有的bug。 – Wil

+0

這對Stack Overflow來說有點過於寬泛。你嘗試過什麼嗎?當你陷入困境時,我們會在這裏幫助你,但是爲了討論主題,這個問題需要徵集一些比圖書館更多的東西。你提到的是什麼具體問題? –

回答

1

我其實剛剛在這個問題上最近工作。我讓你用我寫的代碼。它的功能是一旦用戶瀏覽一個文件,另一個上傳框彈出,等等。然後,我使用phpmailer將這些文件作爲電子郵件附件發送。查找PHPMailer的更多細節...

添加更多的上傳域。(放在HEAD)

<script type="text/javascript"> 
function addElement() 
     { 
      var ni = document.getElementById('org_div1'); 
      var numi = document.getElementById('theValue'); 
      var num = (document.getElementById('theValue').value -1)+ 2; 
      numi.value = num; 
      var newDiv = document.createElement('div'); 
      var divIdName = num; newDiv.setAttribute('id',divIdName); 

      newDiv.innerHTML = '<input type="file" class="fileupload" size="80" name="file' + (num) + '" onchange="addElement()"/> <a class="removelink" onclick=\'removeElement(' + divIdName + ')\'>Remove This File</a>'; 

      // add the newly created element and it's content into the DOM 
      ni.appendChild(newDiv); 
     } 

     function removeElement(divNum) 
     { 
      var d = document.getElementById('org_div1'); 
      var olddiv = document.getElementById(divNum); 

      d.removeChild(olddiv); 
     } 

    </script> 

的HTML中的JavaScript ..

<div id="org_div1" class="file_wrapper_input"> 

         <input type="hidden" value="1" id="theValue" /> 
<input type="file" class="fileupload" name="file1" size= 
"80" onchange="addElement()" /></div> 

的process.php頁。注意:必須編輯!搜索phpmailer並下載他們的class.phpmailer.css類。編輯文件中的配置。創建並「上傳」目錄。

<?php 
require("css/class.phpmailer.php"); 





//Variables Declaration 
$name = "Purchase Form"; 
$email_subject = "New Purchase Ticket"; 

$body = "geg"; 

foreach ($_REQUEST as $field_name => $value){ 
if (!empty($value)) $body .= "$field_name = $value\n\r"; 
} 
$Email_to = "[email protected]"; // the one that recieves the email 
$email_from = "No reply!"; 
// 
//==== PHP Mailer With Attachment Func ====\\ 
// 
// 
// 
$mail = new PHPMailer(); 

$mail->IsQmail();// send via SMTP MUST EDIT!!!!! 
$mail->From = $email_from; 
$mail->FromName = $name; 
$mail->AddAddress($Email_to); 
$mail->AddReplyTo($email_from); 

foreach($_FILES as $key => $file){ 
$target_path = "uploads/"; 
$target_path = $target_path .basename($file['name']); 

if(move_uploaded_file($file['tmp_name'], $target_path)) { 
echo "the file ".basename($file['name'])." has been uploaded"; 
}else { 
echo "there was an error"; 
} 
$mail->AddAttachment($target_path); 
} 
$mail->Body = $body; 
$mail->IsHTML(false); 
$mail->Subject = $email_subject; 
if(!$mail->Send()) 
{ echo "didnt work"; 
} 
else {echo "Message has been sent";} 


foreach($_FILES as $key => $file){ 
$target_path = "uploads/"; 
$target_path = $target_path .basename($file['name']); 
unlink($target_path);} 
} 










?> 

讓我知道如果您有任何問題!

+0

感謝您的幫助,但添加文件後,會發生什麼?我創建了上傳文件夾,但是當我添加process.php文件和class.phpmailer.php文件時,它仍然無法工作。我需要做些什麼才能使其發揮作用? –

+0

@Pete那麼我給你的html應該在action.php的表單標籤內...你是否這樣做?並且在process.php中,notice是「php require(」css/class.phpmailer.php「);'確保它是正確的路徑! –

相關問題