php强制下载文件函数

yipeiwu_com5年前PHP代码库

本文实例为大家分享了php强制下载文件函数,供大家参考,具体内容如下

public function down()

{
  $id = $this->_get('id');
  $M = M("downloads");
  $data=$M->where("id=$id and status=1")->find();
  !$data && exit;
  $filename = iconv('UTF-8','GBK',$data['filename']);
  $savename = $data['savename']; 
  $myfile = $data[url] ? $data[url] : 'Uploads/file/'.$savename;
  if(file_exists($myfile)){
    $M->where("id=$id")->setInc('downloads');
    $file = @ fopen($myfile, "r");
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=" .$filename );
    while (!feof($file)) {
    echo fread($file, 50000);
    }
    fclose($file);
    exit;
  }else{
    echo '文件不存在!';

  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【宜配屋www.yipeiwu.com】。

相关文章

Laravel 5.3 学习笔记之 安装

1、服务器要求 Laravel 框架有对服务器有少量要求,当然,Laravel Homestead 已经满足所有这些要求,所以我们强烈推荐使用 Homestead 作为 Laravel...

jQuery+PHP发布的内容进行无刷新分页(Fckeditor)

jQuery+PHP发布的内容进行无刷新分页(Fckeditor)

这篇文章将使用jQuery,并结合PHP,将Fckeditor发布的内容进行分页,并且实现无刷新切换页面。  本文假设你是WEB开发人员,掌握了jQuery和PHP相关知识,并...

php正则preg_replace_callback函数用法实例

本文实例讲述了php正则preg_replace_callback函数的用法。分享给大家供大家参考。具体实现方法如下: php正则表达式功能强大,本范例演示了preg_replace_c...

PHP print类函数使用总结

复制代码 代码如下:<?php /*************by garcon1986*********/ //print和echo的区别: //1. echo可以输入多个字符串,...

PHP header函数分析详解

在php语言中,header()这个函数很有用的,尤其在用到ajax时候,他会帮你解决一些意想不到的问题。下面是header的一些详细讲解。希望对phper有帮助 复制代码 代码如下:...