Download file with PHP

by Little Bear @23 ก.พ. 51 09:23 ( IP : 61...220 ) | Tags : PHP , Programing

Example PHP code for download file that nobody know your file url

ตัวอย่าง code สำหรับดาวน์โหลดไฟล์โดยไม่ให้ใครรู้ url ของไฟล์ที่กำลังดาวน์โหลด


// 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" ""> <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>'; ?>