search
HomeBackend DevelopmentPHP TutorialHow to generate QR code and download in php

这篇文章主要介绍了php二维码生产以及下载实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了php二维码生成以及下载的具体代码,供大家参考,具体内容如下


<?php
 
//引入phpqrcode库文件
define(&#39;IN_ECS&#39;, true);
 
require(dirname(__FILE__) . &#39;/includes/init.php&#39;);
include(&#39;includes/phpqrcode.php&#39;);
 
// 二维码数据
$data = &#39;http://www.baidu.com&#39;;
$filename = &#39;shopEwm/&#39;.&#39;baidu.png&#39;;
 
//down_file(&#39;baidu.png&#39;,BASE_PATH);
setShopEwm($data,$filename);
 
//生成二维码图片
function setShopEwm($data,$filename){
// 纠错级别:L、M、Q、H
 
  $errorCorrectionLevel = &#39;L&#39;;
  // 点的大小:1到10
  $matrixPointSize = 4;
  //创建一个二维码文件
  QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
  //输入二维码到浏览器
  //QRcode::png($data);
}
//下载二维码图片
function down_file($file_name){
  $file_sub_dir = str_replace(&#39;\\&#39;,&#39;/&#39;,realpath(dirname(__FILE__).&#39;/&#39;))."/shopEwm/";
  //原因 php文件函数,比较古老,需要对中文转码 gb2312
  $file_name=iconv("utf-8","gb2312",$file_name);
 
  //绝对路径
  $file_path=$file_sub_dir.$file_name;
  //1.打开文件
  if(!file_exists($file_path)){
    echo "文件不存在!";
    return ;
  }
 
  $fp=fopen($file_path,"r");
  //2.处理文件
  //获取下载文件的大小
  $file_size=filesize($file_path);
 
  /* if($file_size>30){
 
  echo "<script language=&#39;javascript&#39;>window.alert(&#39;过大&#39;)</script>";
  return ;
  } */
 
  //返回的文件
  header("Content-type: application/octet-stream");
  //按照字节大小返回
  header("Accept-Ranges: bytes");
  //返回文件大小
  header("Accept-Length: $file_size");
  //这里客户端的弹出对话框,对应的文件名
  header("Content-Disposition: attachment; filename=".$file_name);
 
  //向客户端回送数据
 
  $buffer=1024;
  //为了下载的安全,我们最好做一个文件字节读取计数器
  $file_count=0;
  //这句话用于判断文件是否结束
  while(!feof($fp) && ($file_size-$file_count>0) ){
    $file_data=fread($fp,$buffer);
    //统计读了多少个字节
    $file_count+=$buffer;
    //把部分数据回送给浏览器;
    echo $file_data;
  }
 
  //关闭文件
  fclose($fp);
 
}

The above is the detailed content of How to generate QR code and download in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.