2014-10-06 112 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);   
相關問題