- Forums
- PHP
- How To Force File Download Windows When Click On Link
This Page Contains information about How To Force File Download Windows When Click On Link By wallpaperama in category PHP with 2 Replies. [1231], Last Updated: Sat May 18, 2024
wallpaperama
Tue Dec 23, 2008
2 Comments
2639 Visits
often when i go to google.com and seach for images, sometimes, i find some pictures from blogger.com and when i click on the See full size image link, i get a popup windows to download the file instead of taking me to the the location where the file is:
you can try it yourself: go to
http://images.google.com/images?gbv=2&hl=en&q=site%3Ablogger.com+blogger+logo&btnG=Search+Imageswell, i often wondered how they do that? i would like to know how they do that so i can do it on my website too.
ok, so im going to show you an example. i will make a short tutorial so you can also understand how they do it. however, one thing is required. and that is for you to have PHP. if you dont have PHP on your wesite, then this wont work. i dont know how to do this on microsoft windows server. im using a linux server with apache and php. so here we go.
step1. the first step is to create a directory where we can put our php file. so create a directory in your website and name it:
teststep2. now we are going to create a file called
image.php - open your text editor. im using a windows xp computer to write this tutorial, so ill be using notepad.
step3. copy and paste the code below into a blank notepad:
image.php
<?
# PATH TO THE IMAGE FILE IN YOUR WEBSITE
$file_name = 'wallpaperama.gif';
# USE THE HEADER() FUNCTION TO TELL THE BROWSER ON THE USERS THAT THIS IS AN ATTACHEMENT FILE TO FORCE TO DOWNLOAD
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
?>
step4. after you have copy and paste the code above, save the file as
image.phpstep5. now get any .gif image file you want and put it in our
test directory and change the name of the file to
wallpaperama.gifstep 6. now we are going to create a simple file to link to our
image.php file. so copy and paste the following code to a new blank notepad:
index.php
<h1>Wallpaperama Image Tutorial</h1><hr />
<p><a href="image.php">Click on this link to download wallpaperama.gif</a></p>
<p>When you click on the the link above, you should get a little popup window asking you to download the image file instead of viewing it. now if you just want to view the file, then click on the following link:</p>
<p><a href="wallpaperama.gif">Click on this link to see wallpaperama.gif</a></p>
<p>This is just a simple script, however, once you understand the concept here, you can make it so that even if you click on the wallpaperama.gif, you can still force the user to download the file instead of vieiwing. for example. if the referer is not coming from your website, force the user to download the file, but if they are coming from your site, then allow them to view the image file fom their browser. another way you can use this is to count how many times people are downloading this file. using mysql database you can keep a track of how many donwloads and from where.</p>
<p align="center">Script by Wallpaperama.com</p>
step 7. once you have copy and save the code above, save the file as
index.phpstep 8. now upload the directory
test to your PHP website and the three files:
image.phpindex.php wallpaperama.gifstep 6. now go to your website with your browser and open the
test directory: example:
http://www.yoursite.com/test/index.php and see it in action.hope this helps
searched by: image force download blogger.com HTTP response header
leapbrowser search engine.