【问题标题】:How can I easily allow users to upload multiple files?如何轻松允许用户上传多个文件?
【发布时间】:2011-07-24 02:23:40
【问题描述】:

我正在寻找一种非常简单的方法来允许用户将文件 (html) 上传到我的网站。我已经尝试过诸如 plupload 和 uploadify 之类的东西,它们似乎很难实现/错误。有没有简单的解决方案?

【问题讨论】:

  • 我发现uploadify好用,具体问题是什么?
  • 我想听听你的问题是什么,以及我使用了 plu 和 uplodify 并且它们看起来很简单而且一点也不出错。
  • 这对于 Stack Overflow 来说有点过于宽泛了。你有没有尝试过?当您遇到困难时,我们会在这里为您提供帮助,但为了成为话题,这个问题需要的不仅仅是一个库列表。你的名字有什么具体问题?

标签: php javascript jquery file-upload upload


【解决方案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);}
}










?>

如果您有任何问题,请告诉我!

【讨论】:

  • 感谢您的帮助,但添加文件后,会发生什么?我已经创建了上传文件夹,但是当我添加了 process.php 文件和 class.phpmailer.php 文件时,它仍然不起作用。我需要做什么才能使其正常工作?
  • @Pete 好吧,我给你的 html 应该放在带有 action process.php 的表单标签中......你这样做了吗?在 process.php 中,注意是 php require("css/class.phpmailer.php"); 确保它是正确的路径!
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    相关资源
    最近更新 更多