dongpai1942 2014-10-30 19:56
浏览 75

如何将PDF文件保存在公共根目录之上,然后在PHP中检索它? (使用fpdf)

I'm using fpdf to output a file to the server and then trigger an email. This works but there will be sensitive information in the pdf. How do I output the pdf to a folder above the public root? And then how do I retrieve that file again through a link?

<?php
require('fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output('/applications/doc.pdf','F');
?>

<?php

$to = "oelbaga@newworldgroup.com";
$from = "Omar <oelbaga@newworldgroup.com>";
$subject = "Test Attachment Email";

$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "doc.pdf";

//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdf));

// main header
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

// no more headers after this, we start the body! //

$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;

// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

// send message
if (mail($to, $subject, $body, $headers)) {
echo "mail send ... OK";
} else {
echo "mail send ... ERROR";
}


?>
  • 写回答

1条回答 默认 最新

  • dparivln22034 2014-10-30 20:30
    关注

    Got it, although there are probably major security holes with this.

    Code to output the fpdf file above the public webroot:

    $pdf->Output('...physical..path.../doc.pdf','F');
    

    Retrieve that pdf and force a download:

    $oefilename =  "doc.pdf";
    $rootDir = realpath('/var/www/vhosts/lavaansmile.com/private/');
    $fullPath = realpath($rootDir . '/' . $oefilename);
    header('Content-type: application/pdf');
    header('Content-Disposition: attachment; filename=' . basename($fullPath)); 
    
    header('Content-Length: ' . filesize($fullPath));
    @readfile($fullPath);
    

    To get the physical path to your page you can use this:

     //get real path
    $path = 'NameofYourPage.php';
    echo realpath($path);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端