2014-10-30 112 views
0

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

<?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 = "[email protected]"; 
$from = "Omar <[email protected]>"; 
$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"; 
} 


?> 
+0

我意识到我可以输出到webroot上面的目录,只需写出真实的物理路径即可。 – omar 2014-10-30 20:12:49

+0

如果php使用'chroot'配置运行,这将不起作用。 (即访问不在脚本基目录路径下的文件) – 0xGiddi 2014-10-30 20:16:16

回答

0

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

的代码输出公众根目录上面的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); 
相关问题