5

I have a script that creates a vCard for members of staff when the 'Add Contact' button is clicked. I have this vCard in a variable, but I'm not really sure what to do with it next.

I take it that my frist step should be to save this file on the server?

I'd like to just have a box pop up and allow people to download and save the vCard, so if the step about is not necessary I'd like to just skip it.

Any pointers here would be appriciated.

Thanks.

David Gard
  • 11,225
  • 36
  • 115
  • 227
  • PHP can be used to output things other than html to users. Send the proper file headers and users will be prompted to download a file instead of getting an HTML page. – DampeS8N Jan 09 '12 at 16:06
  • The more generic version of this question is [Force File Download](http://stackoverflow.com/questions/3718962/force-file-download-in-php). It doesnt address all of your particular questions, so I wont give it as possible duplicate, although this is really borderline. – Gordon Jan 09 '12 at 16:33
  • Thanks Gordon, I wasn't really sure what I was looking for as I've never done this before. I.e. I know I needed to download a file, but I didn't actually think there would be away to do what I wanted without saving to the server first and downloading, so the 'force' bit didn't even enter my thoughts. – David Gard Jan 09 '12 at 17:04

5 Answers5

8

If you want a File Save dialog to pop up when someone requests the export URL, you have to use

header("Content-type:text/vcard; charset=utf-8");
header("Content-Disposition: attachment; filename=vcardexport.vcf");
echo $vCardData;

So No, you dont have to save it as a file on the server first. You can serve it from the variable. Note that you can use this approach for any other data as long as you specify the right MIME Type for Content-Type.

Also see https://en.wikipedia.org/wiki/VCard and https://www.ietf.org/rfc/rfc2183.txt

Gordon
  • 312,688
  • 75
  • 539
  • 559
2

If you have your vcard in a variable, then you can easily force it as a download onto the client with this code:

<?php

header('Content-type: text/vcard');
header('Content-disposition: attachment;filename=vcard.vcf');
echo $vcard_variable;

?>
Aleks G
  • 56,435
  • 29
  • 168
  • 265
1

Try look at the content-disposition header :)

It can force a file download at the client :)

f2lollpll
  • 997
  • 6
  • 15
1

You can just output the vCard from PHP, setting the proper content-type with a response header. This should force a download on the user's browser. I've googled it and found this example.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • Thank you, that example helped alot and other than an oddity with the permalinks on my site, this appears to be working. – David Gard Jan 09 '12 at 17:01
0

If you have the file on the server you can just have a link on the button that points to the file

<a href="location of the vcard file"><img src="button.jpg"></a>

or are you looking for a different delivery method?

Silvertiger
  • 1,680
  • 2
  • 19
  • 32
  • The vCard is being created dynamically when a users clicks the 'Add Content' link in a staff members profile. There is no page reload at present, everything is done through AJAX. – David Gard Jan 09 '12 at 16:11