如何轻松允许用户上传多个文件?

时间:2011-07-24 02:23:40

标签: php javascript jquery file-upload upload

我正在寻找一种非常简单的方法来允许用户将文件(html)上传到我的网站。我尝试过像plupload和uploadify这样的东西,似乎很难实现/ buggy。有没有简单的解决方案?

2 个答案:

答案 0 :(得分:1)

我最近刚刚解决了这个问题。我会让你使用我写的代码。它的作用是用户浏览一个文件,弹出另一个上传框等等。然后,我使用phpmailer将文件作为电子邮件附件发送。查找phpmailer了解更多详情......

添加更多上传字段的javascript ..(放入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 ..

<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 = "blank@blank.com"; // 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);}
}










?>

如果您有任何疑问,请与我联系!!

答案 1 :(得分:0)

查看jQuery Mulitple File Upload Plugin。 PHP文档中的Uploading multiple files页面将让您了解提交的结果的样子以及使用它的一些示例。