Many times, we will come across some situations seeing Download Here Links on so many websites, Will they place actual File in the Download Here Link ?? Answer is No. Then How to acheive this through Programmatically, This Tutorial will guide you how you can develope your own Dynamic Download Here Links, with out giving out access to the actual file name.
Lets get into the discussion.
Inorder to acheive this Dynamic Download Here Link we have to play with header() in PHP.
PHP will give you flexibility to choose your actual content-type .
Lets see two examples How we can obtain this Dynamic Downlod Here link for PDF and EXE ..
Dynamic Download Here link for PDF File
Here is the piece of code
Create Download.php and paste the following code into the file
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php $file="GIVE_PATH_TO_ACTUAL_FILE_with_extension"; header('Content-type: application/pdf'); // It will be called downloaded.pdf header('Content-Disposition: attachment; filename="GIVE_MASKED_NAME"'); // The PDF source is in original.pdf readfile('$file'); ?> |
The Above Code will focus on Only Core activity of Downloading a PDF File. Depending on our requirements we can customize to suit our application requirements.
1. Handling Sessions to give away PDF to the Authenticated users Preventing others from download
2. Custom Naming of File Depending on the Time
Dynamic Download Here Link for EXE File
As same as above Here is the piece
Create Download.php and paste the following code into the file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php $file="ACTUAL_FILE_PATH_FOR_YOUR_EXE_FILE"; header("Content-Disposition: attachment; filename=CUSTOM_NAME_FOR_YOUR_DISPLAY_FILE_NAME" ); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Description: File Transfer"); header("Content-Length: " . filesize($file)); flush(); // this doesn't really matter. $fp = fopen($file, "r"); while (!feof($fp)) { echo fread($fp, 65536); flush(); // this is essential for large downloads } fclose($fp); ?> |
The Above Code is helpful for CORE activity of Download of EXE File.
Customize, your application using above code
Happy Coding !!
Written by : adrevol
( Treats Given : 35 Treats Attended : )

