zoukankan      html  css  js  c++  java
  • PHP文件下载

    步骤

    //流的方式发送给浏览器
    header("Content-Type: application/octet-stream");
    //按照字节的返回给浏览器
    header("Accept-Ranges: bytes");
    //告诉浏览器文件的大小
    header("Accept-Length: ".filesize('文件地址'));
    //以附件的形式发送给浏览器(也就是弹出,下载的对话框)
    header("Content-Disposition: attachment; filename=文件名称");
    
    //打开文件获取文件句柄
    $handle=fopen('文件地址',"r");
    //将文件直接读取完
    echo fread($handle,filesize('文件地址'));
    //一部分一部分的读取
    // while(!feof($handle)){
    //     $content=fread($handle,1024);
    //     echo $content;
    // }
    fclose($handle);
    

    具体实现

    $file_path = '/home/wwwroot/nikon/app.js';
    
    download($file_path);
    
    function download($file_path) {
        //流的方式发送给浏览器
        header("Content-Type: application/octet-stream");
        //按照字节的返回给浏览器
        header("Accept-Ranges: bytes");
        //告诉浏览器文件的大小
        header("Accept-Length: ".filesize($file_path));
        //以附件的形式发送给浏览器(也就是弹出,下载的对话框)
        header("Content-Disposition: attachment; filename=".basename($file_path));
    
        //打开文件获取文件句柄
        $handle=fopen($file_path,"r");
        //将文件直接读取完
        //echo fread($handle,filesize($file_path));
        // 一部分一部分的读取
        while(!feof($handle)){
            $content=fread($handle,1024);
            echo $content;
        }
        fclose($handle);
    }
    

  • 相关阅读:
    PageAdmin Cms系统如何修改点击数(浏览数)
    最简js深浅拷贝说明
    实用的前端网址
    js的一些编码问题
    自己写一个文字过长显示省略号的函数
    垂直居中 和 水平居中
    js原码工具集
    url 、src 、href 的区别
    transform对定位元素的影响
    css实现一色多变化
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/9207546.html
Copyright © 2011-2022 走看看