博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php 实现下载
阅读量:6976 次
发布时间:2019-06-27

本文共 2826 字,大约阅读时间需要 9 分钟。

/*

本文章提供的三款文件下载代码有二款是支持本地服务器文件下载的,另一款支持下载远程服务器上的文件下载到本地哦。
*/
function download($file_dir,$file_name)
//参数说明:
//file_dir:文件所在目录
//file_name:文件名
{
    $file_dir = chop($file_dir);//去掉路径中多余的空格
    //得出要下载的文件的路径
    if($file_dir != '')
    {
        $file_path = $file_dir;
        if(substr($file_dir,strlen($file_dir)-1,strlen($file_dir)) != '/')
            $file_path .= '/';
        $file_path .= $file_name;
    }           
    else
        $file_path = $file_name;   
   
    //判断要下载的文件是否存在
    if(!file_exists($file_path))
    {
        echo '对不起,你要下载的文件不存在。';
        return false;
    }
    $file_size = filesize($file_path);
    header("content-type: application/octet-stream");
    header("accept-ranges: bytes");
    header("accept-length: $file_size");
    header("content-disposition: attachment; filename=".$file_name);
   
    $fp = fopen($file_path,"r");
    $buffer_size = 1024;
    $cur_pos = 0;
   
    while(!feof($fp)&&$file_size-$cur_pos>$buffer_size)
    {
        $buffer = fread($fp,$buffer_size);
        echo $buffer;
        $cur_pos += $buffer_size;
    }
   
    $buffer = fread($fp,$file_size-$cur_pos);
    echo $buffer;
    fclose($fp);
    return true;
}
?>
<?php
$file_name = "info_check.exe";
    $file_dir = "/public/www/download/";
    if (!file_exists($file_dir . $file_name)) { //检查文件是否存在
    echo "文件找不到";
    exit;
    } else {
    $file = fopen($file_dir . $file_name,"r"); // 打开文件
    // 输入文件标签
    header("content-type: application/octet-stream");
    header("accept-ranges: bytes");
    header("accept-length: ".filesize($file_dir . $file_name));
    header("content-disposition: attachment; filename=" . $file_name);
    // 输出文件内容
    echo fread($file,filesize($file_dir . $file_name));
    fclose($file);
    exit;}
?>
<?
// php 文件下载代码:php如何实现文件下载功能(支持远程文件下载)
//如果文件路径是http和ftp,下载代码如下:
$file_name = "info_check.exe";
    $file_dir = " http://www.zzarea.com/";
    $file = @ fopen($file_dir . $file_name,"r");
    if (!$file) {
    echo "文件找不到";
    } else {
    header("content-type: application/octet-stream");
    header("content-disposition: attachment; filename=" . $file_name);
    while (!feof ($file)) {
    echo fread($file,50000);
    }
    fclose ($file);
    }
    ?>
本文来源网页制作教程网www.zzarea.com 原文链接:http://www.zzarea.com/php100/php-1124.html
在调用过程中,会出现
Warning: Cannot modify header information
这个提示。我在网上找了很多种方法只有一种管用。修改php.ini output_suffering = on或者 =4096
后者比前者在下载中速度要快一些。不明白什么原因。
还有ob_start(); ob_end_flush();这个没起作用。
我的写法是。
<?php ob_start(); ?>
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
$filetype = $_REQUEST['filetype'];
//echo $filetype;
$path = dirname(__FILE__)."/http://www.cnblogs.com/seq/";
$durl = $path.$filename;
$filename = 'phpcms2008_o2abf32efj883c91a.iso';
$file = @fopen($durl, 'r');
header("Content-Type: application/octet-stream");
header("Accept-Ranges:    bytes");
header("Accept-Length:    ".filesize($durl));
header("Content-Disposition:    attachment;    filename=".$filename);
echo   fread($file,filesize($durl));
fclose($file);
ob_end_flush();
?>
是不是写错了?

转载于:https://www.cnblogs.com/tjmsz/archive/2012/03/23/2413321.html

你可能感兴趣的文章
Apache的三种工作模式
查看>>
dlib编译成静态库及被其它程序调用
查看>>
UNIX网络编程之epoll的 accept , read , write
查看>>
java事务管理
查看>>
分布式MySQL 数据库
查看>>
MMX指令集系列之一----数据加载与算术运算指令
查看>>
R语言可视化二
查看>>
Python abs() 函数
查看>>
西电网络赛 - C
查看>>
div布局方案整理
查看>>
Python之内置函数再总结
查看>>
同步锁,死锁现象与递归锁,信息量Semaphore.....(Day36)
查看>>
sql server 索引阐述系列三 表的堆组织
查看>>
GD库笔记
查看>>
js函数知识点
查看>>
Centos 配置eth0 提示Device does not seem to be present
查看>>
redis5.0.3单实例简单安装记录
查看>>
metro 微博api开发,post请求
查看>>
C#中UDP数据的发送、接收
查看>>
自己设计大学排名-数据库实践
查看>>