droxy80248 2014-10-06 11:12
浏览 84
已采纳

如何添加下载按钮?

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}
 ";
$data .= "Nom: {$codeContents[0]->Nom} ";
$data .= "{$codeContents[0]->Cognoms}
";
$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条回答 默认 最新

  • douzhuan4406 2014-10-06 11:52
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划