ตัวอย่าง
เช่นเว็บไซต์ชือ https://www.ninenik.com
มีโฟลเดอร์ สำหรับดาวน์โหลดไฟล์ ชื่อ loaddir
path ไฟล์สำหรับดาวน์โหลด คือ https://www.ninenik.com/loaddir/
ให้สร้างไฟล์ ชื่อ index.php ตามโค้ดด้านล่าง
PHP Code ไฟล์ index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php if ( $_GET [ 'dfile' ]){ $file = $_GET [ 'dfile' ]; if (!file) { die ( 'file not found' ); } else { header( "Cache-Control: public" ); header( "Content-Description: File Transfer" ); header( "Content-Disposition: attachment; filename=$file" ); header( "Content-Transfer-Encoding: binary" ); readfile( $file ); } } ?> |
อัพโหลดไฟล์ index.php ไปไว้ในโฟลเดอร์ loaddir
อัพโฟลดไฟล์ทดสอบ เช่น testword.docx ไปในโฟลเดอร์ loaddir
url เรียกไฟล์สำหรับดาวน์โหลด ดังนี้
https://www.ninenik.com/loaddir/?dfile=testword.docx
จะขึ้น การแจ้งเตือน ให้เปิด หรือ บันทึกไฟล์ที่ต้องการดาวน์โหลด
ตัวอย่างที่กล่าวข้างต้นเป็นแนวทาง สามารถนำไปประยุกต์ใช้ เช่น
- สร้าง mod_rewrite จัดรูปแบบ url สำหรับเรียกดาวน์โหลดไฟล์
- เข้ารหัสชื่อไฟล์
เป็นต้น