How does PHP implement the image click Download and save locally? -----This example is a two-dimensional code generation image, and support click to download

Source: Internet
Author: User

###

Today because of work needs, completed a two-dimensional code generation image, and support click to download the

###

Controller files, related codes

1  //generate two-dimensional code2         $url= Action (' Apih5\\[email protected] ', [' provider ' = =$request-Channel]);3 //save QR Code to local and return QR code4         $qrcode=$this->app[' version ']->qrcode ($url);
5 $dir _path= '.. /public/static/versionchannel/';6 $fileName=$request->channel. '. png;7 $qrcode = Base64_decode ($qrcode);8 //Save to local, if the file does not exist, create a new9 $this->app[' Version ']->Saveversionqrcode ($qrcode, $dir _path, $fileName);Ten One //save QR Code to local and return QR code A $qrcode=$this->app[' Version ']->qrcode ($request->channel,$url);

Background implementation of the downloaded controller

1<?PHP2 3 namespace App\http\controllers\apih5;4 5  Useilluminate\http\request;6 7  Useapp\http\requests;8  UseApp\http\controllers\controller;9 Ten classVersioncontrollerextendsController One { A     //version Two-dimensional code download -      Public functionDownload$provider, Request$request) -     { the         //download QR code -         $contenttype= ' Image/jpeg '; -         $dir _path= '.. /public/static/versionchannel/'; -         $fileName=$provider.‘. png; +         $fileurl=$dir _path.$fileName; -  +        Header("Cache-control:private");  A Header ("Content-type: $contenttype"); // set the file types to download  at Header ("Content-length:".) filesize ($fileurl)); // set the file size to download files                      -        Header("Content-disposition:attachment; Filename= ". UrlDecode ($fileName)); // set the file name to download  readfile($fileurl);             -  -     } in}

File where method is located

1  /**2 * Generate two-dimensional code for Apih5/versioncontroller3 * @param [Type] $id [description]4 * @return [Type] [description]5      */6      Public functionQRCode ($url)7     {8         $qrcode=NewQrcodehelper;9         $qrcode=$qrcode-&GT;GETVERSIONCHANNELQR ($url);Ten  One         return  $qrcode; A     } -  -     /** the * Save QR Code to local folder -     */ -      Public functionSaveversionqrcode ($qrcodeData,$dir _path,$fileName) -     { +         //determines whether the directory exists, does not exist, and generates -         if(!file_exists($dir _path) ) { +             mkdir("$dir _path", 0777,true); A         } at         $fileurl=$dir _path.$fileName; -         //Save to local, if the file does not exist, create a new -         file_put_contents($fileurl,$qrcodeData,File_use_include_path); -}

Front desk trying to

Displays the location of the QR code picture

1  <td>23       $version->channel])}} ">4            $version->channel. " PNG ')}} "/>5       </a>6 </td>

How does PHP implement the image click Download and save locally? -----This example is a two-dimensional code generation image, and support click to download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.