【问题标题】:How can I add a download button?如何添加下载按钮?
【发布时间】:2016-11-19 06:47:44
【问题描述】:

我有这个输出二维码的代码:

<?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.'" />';
?>

如何添加下载按钮,以便用户可以将代码下载到计算机? 谢谢,

丹妮

【问题讨论】:

    标签: php download qr-code


    【解决方案1】:

    这更像是一个 HTML 问题,所以让我们开始吧:有一个名为“下载”的属性,您可以像这样使用它:

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

    它以您在此处提供的名称下载文件。因此,如果您服务器上的图像 url 是:wadAHEybwdYRfaedBFD22324Dsefmsf.png,它会将文件下载为“qrcode.png”。不幸的是,并非所有浏览器都支持此功能。另一个简单的解决方法是制作一个将您的文件名作为操作的表单,如下所示:

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

    另一种方法(更多代码)是使用带有一些特定标头的 PHP,如下所示:

    <?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>
    ?>
    

    它适用于大小文件以及许多不同的文件类型。信息在这里找到:http://www.finalwebsites.com/forums/topic/php-file-download

    【讨论】:

    • 谢谢,我采用了 HTML 方式。
    【解决方案2】:

    好吧,你显然是在用你的脚本创建一个二维码的.png 图片文件。所以只需添加图片位置的链接即可:

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

    然而,这只会将用户重定向到浏览器中的图片。 如果您想强制下载,您需要使用 PHP 标头将文件发送到访问者浏览器。

    例如制作一个脚本并将其命名为download_code.php。 然后添加:

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

    而在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);        
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-06
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-03
      相关资源
      最近更新 更多