How can I add a download button?

2019-09-07 13:40发布

I have this code which outputs a QR code:

<?php
include(JPATH_LIBRARIES . '/phpqrcode/qrlib.php');

$db = JFactory::getDbo(); 
$user = JFactory::getUser();

$query = $db->getQuery(true); 
$query->select($db->quoteName(array('Soci', 'Nom', 'Cognoms', 'eCorreu')))
->from($db->quoteName('#__rsform_socis'))
->where($db->quoteName('username') . ' = '. $db->quote($user->username)); 
$db->setQuery($query);
$codeContents = $db->loadObjectList();

$data .= "Soci Nº: {$codeContents[0]->Soci}\n ";
$data .= "Nom: {$codeContents[0]->Nom} ";
$data .= "{$codeContents[0]->Cognoms}\n";
$data .= "e-correu: {$codeContents[0]->eCorreu}";

$tempDir = JPATH_SITE . '/images/'; 
$fileName = 'qr_'.md5($data).'.png'; 
$pngAbsoluteFilePath = $tempDir.$fileName;
$urlRelativeFilePath = JUri::root() .'images/' . $fileName;

if (!file_exists($pngAbsoluteFilePath)) {
QRcode::png($data, $pngAbsoluteFilePath);
} 
echo '<img src="'.$urlRelativeFilePath.'" />';
?>

How can I add a download button so the user can download the code to the computer? Thanks,

Dani

2条回答
爷的心禁止访问
2楼-- · 2019-09-07 14:16

Well, you are obviously creating a .png picture file of the QR code with your script. So simply add a link to the location of the picture:

echo "<a href=\"images/$fileName\">Download</a>";

This will however just redirect the user to the picture in his browser. If you want to force a download you will need to use PHP headers to send the file to the visitors browser.

For example make a script and call it download_code.php. Then add:

echo "<a href=\"download_code.php?filename=$fileName\">Download</a>";

And in the download_code.php:

$handle = fopen("/var/www/yourdomain.com/images/" . $filename, "r");
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
header('Content-Length: ' . filesize("/var/www/yourdomain.com/images" . $filename));

flush();
readfile("/var/www/yourdomain.com/images" . $filename);
fclose($handle);        
查看更多
SAY GOODBYE
3楼-- · 2019-09-07 14:39

This is more of a HTML question, so lets get started: There is an attribute called "download" and you can use it like this:

echo '<a href="'.$urlRelativeFilePath.'" download="qrcode.png">Download QR</a>';

It downloads the file as the name you supply here. So if the image url on your server is: wadAHEybwdYRfaedBFD22324Dsefmsf.png it would download the file as "qrcode.png". Unfortunately this is not supported in all browsers. Another easy fix is to make a form that has your filename as an action like so:

echo '<form method="get" action="'.$urlRelativeFilePath.'"><button type="submit">Download QR Code!</button></form>';

Another way (little more code) to do this is using PHP with some specific headers like this:

<?php

// place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // change the path to fit your websites document structure
$fullPath = $path.$_GET['download_file'];

if ($fd = fopen ($fullPath, "r")) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);
    switch ($ext) {
        case "pdf":
        header("Content-type: application/pdf"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
        break;
        default;
        header("Content-type: application/octet-stream");
        header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    }
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($fd);
exit;
// example: place this kind of link into the document where the file download is offered:
// <a href="download.php?download_file=some_file.pdf">Download here</a>
?>

It works for large and small files and also for many different filetypes. Info found here: http://www.finalwebsites.com/forums/topic/php-file-download

查看更多
登录 后发表回答