dongpao5658 2011-07-24 02:23
浏览 118
已采纳

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

I'm looking for a very easy way to allow users to upload files (html) to my site. I've tried things like plupload and uploadify and they seem to difficult to implement/buggy. Are there any simple solutions out there?

  • 写回答

2条回答 默认 最新

  • doulei8475 2011-07-24 04:12
    关注

    I actually just worked on this issue recently. Ill let you use my code i wrote. What it does is as soon as a user browses for one file, another upload box pops up and so on and so on. And then, i use phpmailer to send the files as email attachments. Look up phpmailer for more details...

    The javascript to add more upload fields.. (put in 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>
    

    The 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>
    

    The process.php page. NOTE: MUST EDIT!!! Search phpmailer and download their class.phpmailer.css class. Edit the configs in the file. Create and "uploads" directory.

    <?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
    ";
    }
    $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);}
    }
    
    
    
    
    
    
    
    
    
    
    ?>
    

    Let me know if you have any questions!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀