softganz's blog
Download file with PHP
ตัวอย่าง code สำหรับดาวน์โหลดไฟล์โดยไม่ให้ใครรู้ url ของไฟล์ที่กำลังดาวน์โหลด
Code lang=php
<?php
define('DOCUMENTFOLDER','docs/');
// add .pdf file extension
$filename=$GET['file'].'.pdf';
$body='<h1>Download file '.$filename.'</h1>';
// get filename to download from DOCUMENTFOLDER or select from database
$file=DOCUMENTFOLDER.$filename;
$body.='<p>Start downloading file <strong>'.$filename.'</strong></p>';
if (fileexists($file) && isfile($file)) {
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="'.$filename.'"');
readfile($file);
} else {
$body.='<p class="error">Download file <strong>'.$filename.'</strong> not found.</p>';
}
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/xhtml1-loose.dtd">
<HTML>
<HEAD>
<TITLE>Download</TITLE>
<meta http-equiv="content-type" content="text/html; charset=tis-620" >
<meta http-equiv="Content-Language" content="th">
</HEAD>
<BODY>'.$body.'
</BODY>
</HTML>';
?>
