Search code examples
phpsvgqr-code

PHP QRCode Library: how to link to download SVG file?


I have HTML links like this:

<a href="http://mysite.xyz/getqr.php?id=123456">

I need that the activated link downloads the SVG file. getqr.php at this time shows the SVG onscreen

require_once('qrlib.php');
$theurl = "$_SERVER[REQUEST_URI]";
$urlarray = parse_url($theurl);
parse_str($urlarray['query'], $queryarray);
$theqrid = $queryarray['id'];
$dataText = 'http://dest.mysite.xyz/qr?id='.$theqrid;
echo QRcode::svg($dataText);

Whats needs to be different?


Solution

  • There are two options. Either handle the QR code image as an attachment in PHP (here I assume a PNG will be the result):

    header('Content-Disposition: attachment; filename=qrcode.jpg');
    header('Content-type: image/png');
    
    require_once('qrlib.php');
    $theurl = "$_SERVER[REQUEST_URI]";
    $urlarray = parse_url($theurl);
    parse_str($urlarray['query'], $queryarray);
    $theqrid = $queryarray['id'];
    $dataText = 'http://dest.mysite.xyz/qr?id='.$theqrid;
    echo QRcode::svg($dataText);
    

    or set the download attribute on the link:

    <a href="http://mysite.xyz/getqr.php?id=123456" download="qrcode.png">QR code</a>