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.

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

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择