Force download of .vcf and other files using php on a Website

I’ve got the resources from a 10 years old german post and still find them very useful.

but first of all, why do you need the script?

Well, I made a small contact site (really small) and wanted to add a link for the Vcard (.vcf), but soon I realised that some browsers try to display it and show a looong page with weird characters. The best way (I think) to solve this problem is to generate a php script which forces the browser to download the file.

The setup:

open the index.htm file and edit it to your needs.

It should look something like this:

<b>save only:</b><BR>
<a href="download.php?what=1">test.zip</a><BR>
<a href="download.php?what=2">test.doc</a><BR>
<a href="download.php?what=3">test.xls</a><BR>
<a href="download.php?what=4">test.pdf</a><BR>
<a href="download.php?what=5">test.gif</a>

while the download.php looks like this:

<?
$files = Array("1" => "test.zip",
"2" => "test.doc",
"3" => "test.xls",
"4" => "test.pdf",
"5" => "test.gif");


$filename = "docs/".$files[$_GET[what]];

header("Content-Type: x-type/subtype");
header("Content-Length: ".filesize($filename));
header("Content-Disposition: attachment; filename=".$files[$_GET[what]]);
readfile($filename);
?>

do you get it? So in order to add  some more files to download straight away, you have to put them into the /docs folder and add them to the download.php file. Then you have to point them out with a html code. Let’ssay that our file is called “contact.vcf” and we want to have it downloaded straight away, we would need to set our files like this:

  1. put the file into the /docs folder
  2. make it available in the download.php file: <code>”6″ => “contact.vcf”; (remember to change the “;” of nr. 5 to  a comma and add a “;” to the last file.
  3. point it out in the html file like this: <a href=”download.php?what=6″>YOUR_FILE_NAME</a>

This should look something like this:

html:<a href="download.php?what=6">YOUR_FILE_NAME</a>

and in php:

<?
$files = Array("1" => "test.zip",
"2" => "test.doc",
"3" => "test.xls",
"4" => "test.pdf",
"5" => "test.gif",
"6" => "contact.vcf");



$filename = "docs/".$files[$_GET[what]];

header("Content-Type: x-type/subtype");
header("Content-Length: ".filesize($filename));
header("Content-Disposition: attachment; filename=".$files[$_GET[what]]);
readfile($filename);
?>

oh well i hope you get it…

here you’ve got the files zipped and ready to use… php-script-force-download (please contact me if the link is dead)(note: you are not able to test the script offline, it just won’t work…sorry)

credits go to: Stuck Mojo from this post: http://www.traum-projekt.com/forum/54-traum-scripts/6574-script-download-ohne-ziel-speichern.html

thanks for reading!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s