Monday 14 May 2012

How to write a php script for file downloading

In this tutorials you will learn how to write a php script for file downloading.If you want to see how to upload a file into a server click here.

<?php 
// Give the path here where your file is located and make a hyper link like <a href='www.yoursite.com?fname=youpic.jpg'>Download image</a>
//$_GET['fname']; will get the file name you given in hyper link and start downloading

$file = 'admin/upload/'.$_GET['fname'];
header("Expires: 0"); 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache"); 
header("Content-type: application/pdf");   
header('Content-length: '.filesize($file)); 
header('Content-disposition: attachment; filename='.basename($file)); 
readfile($file); 
// Exit script. So that no useless data is output-ed. 
exit; 
?> 

 

0 comments:

Post a Comment