我想为 PHP 内部网的用户提供打开/保存 PDF 文件的可能性,这些文件位于 Apache 服务器上的文件夹中。PDF 包含公司私人信息,因此我不想将它们放在网络文件夹中。
echo '<form name="openpdf" method="POST" action="downloadPDF.php">';
echo '<input type="hidden" name="pdf">';
echo'</form>';
<tr>
<td> PDFFile1 </td>
<td><a href=javascript:void(null); onclick='document.openpdf.pdf.value="c:\pdfs\example.pdf";document.openpdf.submit(); return false;'><IMG src="img/pdf.jpg"></a></td></tr>
下载PDF.php:
<?
$pdf=$_POST["pdf"];
if (file_exists($pdf)) {
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename='.basename($pdf));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($pdf));
ob_clean();
flush();
readfile($pdf);
exit;
}
?>
问题是当用户打开/保存文件时,路径指向该文件夹,但在客户端 PC 中而不是在服务器中。