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.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题