dsv768456 2018-06-03 08:35
浏览 69
已采纳

从前端目录列出PDF文件,并允许用户单击文件并发送电子邮件?

I have a directory on my site's server which contains a lot of PDF files.

Is there any way I can display this directory in the front-end of my website? So all the files will be listed in list format. I can then "click" a file, either by a checkbox or something along those lines, and then send the file in an email?

I am using Joomla so this would most likely be a custom module. I already know how to list the files using a query, but the query overloads the server (503 error) as there is actually a lot of files it needs to show.

  • 写回答

1条回答 默认 最新

  • doudu6100 2018-06-13 08:57
    关注

    You are going to want to control how many files get selected. It is a bad idea to display a large quantity of files/check boxes on you page. Especially if there are so many files you can't query them all without getting an error. (Something else is probably going on with that 503 error.) How many pdfs are we talking about? Anyways...

    What happens if you show me just 100 files and I select all 100. Your code is going to send me an email with 100 attachments.(Even 100 will fail, emails are not meant for this.) Then what happens if I really like your pdf's and all I do is sit at my computer all day long, clicking submit over and over sending an email with every press of the button, with 100 attachments every time. Now image 100 people all do that. I'm sure you get the idea, this will destroy your server's resources in a hurry.

    If you can get the file names from the db, that's probably your best method. I would paginate the results so say only 20 files are shown at once. I would only let the user select a maximum amount of files per submit as there is most likely a limit to what an email can do. I would even control how often the user could perform this function in a given period of time in order to prevent abuse.

    I would also look at just directly downloading the files from your server to the user's computer and completely bypass the email thing all together unless that is not within your current project's goal.

    But those are all topics to keep in the back of your mind as you build this. But to get a proof of concept going. Check out below.

    Step 1 - Download PHPMailer and get it going so you can call $mail = new PHPMailer; on a page and it does not error. Here is a link:

    PHPMailer

    Step 2 - Search your folder structure or db and return an array containing the paths to the files.

    Step 3 - Loop over the array and populate your check boxes.

    Step 4 - Create PHPMailer object and add the object properties - send the email.

    require('path/to/PHPMailerClass'); //Or the path to the autoloader if that is how you installed.
    
    $path = '/root/path/to/pdfFiles/';
    $array = array_map('basename', glob($path . '*.pdf')); //This will return all pdf files in the directory.
    
    echo
    '<form action="" method="post">';
    
    foreach($array as $file){
    
       echo '<input name="fileName[]" type="checkbox" value="' . $file . '">' . $file . '<br>';
    
    }
    
    echo '<input name="send_email" type="submit" value="Send Email">';
    
    echo
    '</form>';
    
    //You will need an array containing the user's information.
    $userData = array(
    
      'name' => 'John Doe',
      'email' => 'John.Doe@example.com'
    
    );
    
    if(isset($_POST['send_email']) && $_POST['send_email']){ //Make sure they hit submit.
    
      if(isset($_POST['fileName']) && $_POST['fileName']){ //Make sure they selected a file.
    
        if(count($_POST['fileName']) > 5){
    
          echo 'You can only select 5 files.';
          exit();
        }
    
        //Set up the email.
        $mail = new PHPMailer;
        $mail->setFrom('from@example.com', 'Your Name');
        $mail->addAddress($userData['email'], $userData['name']);
        $mail->Subject  = 'This is how to send attachments.';
        $mail->isHTML(true);
        $mail->Body = $userData['name'] . ',<br>' .
        'Here are the files you selected!';
    
        //Here is the attachment part.
        foreach($_POST['fileName'] as $file){ //Loop across the selected files.
          $mail->AddAttachment($path . $file, $file, 'base64', 'application/pdf');  //Attach the file.
        }
    
        if(!$mail->send()) { //Send the mail.
          echo 'Message was not sent.';
          echo 'Mailer error: ' . $mail->ErrorInfo;
        } else {
          echo 'Message has been sent.';
        }
    
    }else{
    
      echo 'No file was selected.';
    
      }
    
    }
    

    If you are testing on a localhost setup you may have to do some trouble shooting to get the emails to send. I'm not experienced with that so search google and SO to problem solve that.. If after some good attempts to make it work and you can't figure it out, post another question.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥30 德飞莱51单片机实现C4炸弹
  • ¥50 CrossLink-LIF-MD6000 型 FPGA 的 CMOS 转 MIPI D-PHY IP 核功能使用异常