Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP save file to users computer

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.

like image 933
David Gard Avatar asked Dec 21 '22 03:12

David Gard


2 Answers

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

like image 56
Gordon Avatar answered Dec 24 '22 01:12

Gordon


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;

?>
like image 21
Aleks G Avatar answered Dec 24 '22 03:12

Aleks G