Dynamically create a vCard file

Hi Community and Support Team :wave:t2:!

Is there a way to create a file with a .vcf extension and store it inside the corresponding content folder?

I have a function that generates a file with the page information. But I have no idea how I can save it in the folder.

Route:

site/controllers/card.php

Code:

<?php
    return function ($page) {
          $name = $page->name();
          $email = $page->email();
          $phone = $page->phone();

          function generate_vcard(){
              // Code that I already have solved
          }

          $vCard = generate_vcard();

           /* 
               How can I store the vCard file that
               I generate with my function in the content folder?
          */


           return [
               'vCard' => $vCard
           ];
    };
?>

Result:

πŸ“ content
└─ πŸ“ cards
       └─ πŸ“ 0_card
                   └─ πŸ“„ vcard_file_created_automatically.vcf

Template:

<h1><?= $page->name(); ?></h1>

<a href="<?= $vCard->toFile()->url(); ?>" download target="_blank">
  Download vCard
</a>

I await your response and help please.

Thank you very much.

Regards!

Maybe this…

Kirby has other helpers that will help get the path to the relevant content folder.

1 Like

May I ask why you are doing this in your controller? This will try to create this file every time the page is loaded, while you only need to recreate the file when the content changes. So it would make more sense to do this within page.update: after hook.

Hi @texnixe !

OK you are right. Thank you very much for the observation and response. I will review the event page.update:after that you indicate to adapt the solution to my code.

Do you know if there is any way to indicate that the file is created, it is stored in the directory of the active page?

Regards!

You can throw an exception from your hook if the file could not be created.

Do you really need a file? Or do you want people just to be able to download it?

I did a .vcf download recently: VCF / Vcard generation in template
You can link to a virtual file and it triggers the vcf template to generate a vcf with the latest data from the page.

Maybe it helps.

1 Like

Hi @stffr !

I need users to be able to download the vcard file that is generated from the content of a page.

Ok, thank you very much for your answer and help. I will review the information you share with me.

Thank you very much.

Regards!

Hi @stffr !

If the reference you shared helped me a lot.

File: card.vcard.php


    // HEADERS
	kirby()->response()->type('text/x-vcard');
	header('Content-Type: text/x-vcard');
	header('Content-Disposition: attachment; filename="vcard.vcf"');

	// GENERATE
	$vCard = "BEGIN:VCARD";
	$vCard .= "VERSION:3.0";
	$vCard .= "N:".$data['firstname']."\n";
	$vCard .= "FN:".$data['name']."\n";
	$vCard .= "TITLE:".$data['position']."\n";
	$vCard .= "ORG:".$data['company']."\n";
	$vCard .= "URL:".$data['website']."\n";
	$vCard .= "EMAIL;TYPE=INTERNET:".$data['email']."\n";
	$vCard .= "TEL;TYPE=voice,cell,pref:".$data['phone']."\n";
	$vCard .= "END:VCARD";

	// RETURN
	echo $vCard;

	exit();

Thank you very much.

Regards!

1 Like