我有这个输出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.'" />';
?>
如何添加下载按钮,以便用户可以将代码下载到计算机? 谢谢,
达尼
答案 0 :(得分: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);
答案 1 :(得分:1)
这更像是一个HTML问题,所以让我们开始吧:有一个名为“download”的属性,您可以像这样使用它:
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