PHP イントラネットのユーザーが、Apache サーバー上のフォルダーにある PDF ファイルを開いたり保存したりできるようにしたいと考えています。PDF には会社の個人情報が含まれているため、Web フォルダーに配置したくありません。
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 内にあることです。