2014-10-06 104 views
0

我有这样的代码,其输出的QR码:如何添加下载按钮?

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

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

达尼

回答

1

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

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

它下载文件作为您在这里提供的名称。因此,如果您的服务器上的图片网址为: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

+0

谢谢,我采取了HTML的方式。 – 2014-10-06 12:02:52

1

那么,你显然是建立QR码与您的脚本.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);   
相关问题