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

    php下载文件的代码示例

    高洛峰高洛峰2016-12-23 12:46:11原创565
    <?php 
    $file = 'monkey.gif'; 
    
    if (file_exists($file)) { 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename='.basename($file)); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Pragma: public'); 
    header('Content-Length: ' . filesize($file)); 
    ob_clean(); 
    flush(); 
    readfile($file); 
    exit; 
    } 
    ?>

    以上代码是下载代码
    接下来贴一段在线预览pdf文件的代码

    <?php 
    public function fddAction() 
    { 
    // get attachment location 
    $attachment_location = $_SERVER["DOCUMENT_ROOT"] . "/pdf/fdd/sample.pdf"; 
    
    if (file_exists($attachment_location)) { 
    // attachment exists 
    
    // send open pdf dialog to user 
    header('Cache-Control: public'); // needed for i.e. 
    header('Content-Type: application/pdf'); 
    header('Content-Disposition: inline; filename="sample.pdf"'); 
    readfile($attachment_location); 
    die(); // stop execution of further script because we are only outputting the pdf 
    
    } else { 
    die('Error: File not found.'); 
    } 
    } 
    ?>

    更多php下载文件的代码示例相关文章请关注PHP中文网!

    php入门到就业线上直播课:查看学习

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:下载文件
    上一篇:php强制下载文件函数 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• 工具包分享:PHP实现滑块验证图片• 请教QQ互联的代码是如何写的? • 没有上载的分数了,vip次数也用完了啊 • PHP登录跳转,该怎么处理 • 注册信息添加到数据库,该怎么处理
    1/1

    PHP中文网