Home » 经验&资源分享 » ThinkPHP点击下载图片

ThinkPHP点击下载图片

昨天我写了一篇关于如何在ThinkPHP中显示图表的文章,今天分享下关于在ThinkPHP中如何点击下载图片。

在项目中的Action目录下新建一个downAction.class.php的文件,输入如下代码

<span id="lnum1" style="color: #606060;"> 1:</span> &lt;?php

<span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #0000ff;">class</span> downAction <span style="color: #0000ff;">extends</span> Action{

<span id="lnum3" style="color: #606060;"> 3:</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> downimg(){

<span id="lnum4" style="color: #606060;"> 4:</span>             $filename=$_GET[<span style="color: #006080;">"filename"</span>];

<span id="lnum5" style="color: #606060;"> 5:</span>             <span style="color: #0000ff;">if</span>(!$filename)halt(<span style="color: #006080;">'页面不存在'</span>);

<span id="lnum6" style="color: #606060;"> 6:</span>             $file = $filename .<span style="color: #006080;">".png"</span>;

<span id="lnum7" style="color: #606060;"> 7:</span>             <span style="color: #0000ff;">if</span>(file_exists($file)) {

<span id="lnum8" style="color: #606060;"> 8:</span>             header(<span style="color: #006080;">'Content-Description: File Transfer'</span>);

<span id="lnum9" style="color: #606060;"> 9:</span>             header(<span style="color: #006080;">'Content-Type: application/octet-stream'</span>);

<span id="lnum10" style="color: #606060;"> 10:</span>             header(<span style="color: #006080;">'Content-Disposition: attachment; filename='</span>.basename($file));

<span id="lnum11" style="color: #606060;"> 11:</span>             header(<span style="color: #006080;">'Content-Transfer-Encoding: binary'</span>);

<span id="lnum12" style="color: #606060;"> 12:</span>             header(<span style="color: #006080;">'Expires: 0'</span>);

<span id="lnum13" style="color: #606060;"> 13:</span>             header(<span style="color: #006080;">'Cache-Control: must-revalidate, post-check=0, pre-check=0'</span>);

<span id="lnum14" style="color: #606060;"> 14:</span>             header(<span style="color: #006080;">'Pragma: public'</span>);

<span id="lnum15" style="color: #606060;"> 15:</span>             header(<span style="color: #006080;">'Content-Length: '</span> . filesize($file));

<span id="lnum16" style="color: #606060;"> 16:</span>             ob_clean();

<span id="lnum17" style="color: #606060;"> 17:</span>             flush();

<span id="lnum18" style="color: #606060;"> 18:</span>             readfile($file);

<span id="lnum19" style="color: #606060;"> 19:</span>             <span style="color: #0000ff;">exit</span>;

<span id="lnum20" style="color: #606060;"> 20:</span>       }

<span id="lnum21" style="color: #606060;"> 21:</span> }

<span id="lnum22" style="color: #606060;"> 22:</span> ?&gt;

然后在模板页面中添加引用并传入参数即可

<span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">a</span> <span style="color: #ff0000;">href</span><span style="color: #0000ff;">="{:U("</span>/<span style="color: #ff0000;">down</span>/<span style="color: #ff0000;">downimg</span>?<span style="color: #ff0000;">filename</span>=<span style="color: #ff0000;">example112</span><span style="color: #0000ff;">")}"</span><span style="color: #0000ff;">&gt;</span>下载图片<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">a</span><span style="color: #0000ff;">&gt;</span>

Continue reading...
分享