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


How save a PDF file above public root and then retrieve it in PHP? (using fpdf)

我使用fpdf将文件输出到服务器,然后触发电子邮件。这是可行的,但pdf中会有敏感信息。如何将pdf输出到公共根目录上方的文件夹?然后我如何通过链接再次检索该文件?

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

?>

明白了,尽管这可能存在重大的安全漏洞。

在公共webroot上输出fpdf文件的代码:

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

检索pdf并强制下载:

$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);

要获得你的页面的物理路径,你可以使用这个:

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