php文件下载类代码,php文件下载处理类

php中文网
发布: 2016-07-25 08:51:27
原创
1273人浏览过
  1. /*

  2. * 功 能: 文件下载
  3. */
  4. /**
  5. * 使用方法:$download = new download();
  6. * //设置参数
  7. * $download->set_file_dir("c:/");
  8. * $download->set_file_name("boot.ini");
  9. * $download->set_read_bytes(1024);
  10. * //文件下载处理操作
  11. * $download->deal_with();
  12. * //判断文件是否存在
  13. * if($download->is_file_exist()){
  14. *echo "file_exist";
  15. *}else{
  16. *echo "file_not_exist";
  17. *}
  18. */
  19. class download {
  20. private $file = null;//文件句柄
  21. private $file_dir = "";//文件所在的目录
  22. private $file_name = "";//文件名称
  23. private $file_exist = false;//表示文件是否存在, 缺省为不存在
  24. private $read_bytes = 0;//文件读取字节数
  25. private $mode = "r";//文件的访问类型
  26. public function __construct(){

  27. }
  28. public function __destruct(){

  29. if(null != $this->file){
  30. fclose($this->file);
  31. }
  32. }
  33. /**

  34. * 文件下载处理的操作
  35. */ bbs.it-home.org
  36. public function deal_with(){
  37. //文件全路径
  38. $file_path = $this->file_dir . $this->file_name;
  39. //检查文件是否存在

  40. if (file_exists($file_path)) {
  41. $this->file_exist = true;

    立即学习PHP免费学习笔记(深入)”;

  42. // 打开文件

  43. $this->file = fopen($file_path, $this->mode);
  44. // 输入文件标签

    牛NIUCMS本地O2O系统
    牛NIUCMS本地O2O系统

    牛NIUCMS本地O2O系统是一个以php+mysql进行开发的o2o网站系统。NIUCMS是一款强大的网站管理系统。支持智慧城市、智慧小区、智慧乡村、本地生活门户、本地O2O平台的构建。请注意以下几点:1、这套源码必须要服务器支持伪静态,是支持.htaccess规则的伪静态,一般Apache服务器支持,别搞的下载回去以后说什么缺 少文件,其实源码并非缺少文件。2、这套源码请在php 5.4环境下

    牛NIUCMS本地O2O系统 0
    查看详情 牛NIUCMS本地O2O系统
  45. Header("Content-type: application/octet-stream");
  46. Header("Accept-Ranges: bytes");
  47. Header("Accept-Length: " . filesize($file_path));
  48. Header("Content-Disposition: attachment; filename=" . $this->file_name);
  49. // 输出文件内容

  50. while(!feof($this->file))
  51. {
  52. $out = fread($this->file, $this->read_bytes);
  53. if(!get_magic_quotes_gpc())
  54. {
  55. echo $out;
  56. }
  57. else
  58. {
  59. echo stripslashes($out);
  60. }
  61. }
  62. //echo fread($file, filesize($file_dir . $file_name));
  63. }
  64. }
  65. /**

  66. * 返回值为 true 或 false, 通过其值判断文件是否存在
  67. */
  68. public function is_file_exist(){
  69. return $this->file_exist;
  70. }
  71. /**

  72. * 参数类型为字符串
  73. */
  74. public function set_file_dir($file_dir=""){
  75. $this->file_dir = $file_dir;
  76. }
  77. /**

  78. * 参数类型为字符串
  79. */
  80. public function set_file_name($file_name=""){
  81. $this->file_name = $file_name;
  82. }
  83. /**

  84. * 参数类型为整型
  85. */
  86. public function set_read_bytes($read_bytes=1024){
  87. $this->read_bytes = $read_bytes;
  88. }
  89. }
  90. ?>
复制代码


PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号