SoftGanz

Web developer gang.

softganz's blog

Download file with PHP

by @February,23 2008 09.23 ( IP : 61...220 )

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

ตัวอย่าง 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>'
;
?>

แสดงความคิดเห็น

« 1789
หากท่านไม่ได้เป็นสมาชิก ท่านจำเป็นต้องป้อนตัวอักษรของ Anti-spam word ในช่องข้างบนให้ถูกต้อง
Bold Italic Underline Left Center Right Ordered List Bulleted List Horizontal Rule Hyperlink Text Color :)

Main Menu

My recomment topics

Hot topics

Download Firefox and add Thai Extension (Source from osdev)

Links

PageRank Plus - Google PageRank checking, with a twist.
query time 37.569 ms.
request time 64.474 ms.