• 技术文章 >后端开发 >php教程

    php文件下载 重命名

    不言不言2018-04-20 15:04:26原创1993

    这篇文章介绍的内容是关于php文件下载 重命名,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

    php文件下载 重命名

    下载本地文件

    $file_url = “./本地路径”
    $out_filename = ‘下载后自动保存的名字’; 
    if(!file_exists($file_url)) {
        echo "不存在";
    }else{
           header('Accept-Ranges: bytes');
           header('Accept-Length: ' . filesize( $file_url ));
           header('Content-Transfer-Encoding: binary');
           header('Content-type: application/octet-stream');
           header('Content-Disposition: attachment; filename=' . $out_filename);
           header('Content-Type: application/octet-stream; name=' . $out_filename);
           if(is_file($file_url) && is_readable($file_url)){
                $file = fopen($file_url, "r");
                echo fread($file, filesize($file_url));
                fclose($file);
           }
    }

    下载远程文件

    $file_ur = ‘远程文件地址’; 
    $out_filename='下载后自动保存的文件名';
    $file = @fopen($file_url, "r");
    if($file){
        $content="";
        while(!feof($file)){//测试文件指针是否到了文件结束的位置 
            $data=fread($file,1024); 
            $content.=$data;
        }
        fclose($file); 
        $filesize = strlen($content); 
        header('Accept-Ranges: bytes');
        header('Accept-Length: ' . $filesize);
        header('Content-Transfer-Encoding: binary');
        header('Content-type: application/octet-stream');
        header('Content-Disposition: attachment; filename=' . $out_filename);
        header('Content-Type: application/octet-stream; name=' . $out_filename);
            echo $content;
    }else{
        echo "文件不存在";
    }

    相关推荐:

    php文件上传error的错误类型

    以上就是php文件下载 重命名的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:文件下载 php 重命名
    上一篇:php 时间函数strtotime 使用详解 下一篇:PHP结合Redis来限制用户或者IP某个时间段内访问的次数
    千万级数据并发解决方案

    相关文章推荐

    • 100道常见PHP面试题(附解析),增强你的知识储备!• PHP重定向的3种方式_PHP• rephactor 优秀的PHP的重构工具_PHP• php笔记之常用文件操作_PHP• php 连接mssql数据库 初学php笔记_PHP
    1/1

    PHP中文网