douzhenggui8171 2010-01-08 16:12
浏览 136
已采纳

PHP输出文件在磁盘上到浏览器

I want to serve an existing file to the browser in PHP. I've seen examples about image/jpeg but that function seems to save a file to disk and you have to create a right sized image object first (or I just don't understand it :))

In asp.net I do it by reading the file in a byte array and then call context.Response.BinaryWrite(bytearray), so I'm looking for something similar in PHP.

Michel

  • 写回答

5条回答 默认 最新

  • dsfsdfsdfsdf6455 2010-01-08 16:14
    关注

    There is fpassthru() that should do exactly what you need. See the manual entry to read about the following example:

    <?php
    
    // open the file in a binary mode
    $name = './img/ok.png';
    $fp = fopen($name, 'rb');
    
    // send the right headers
    header("Content-Type: image/png");
    header("Content-Length: " . filesize($name));
    
    // dump the picture and stop the script
    fpassthru($fp);
    exit;
    
    ?>
    

    See here for all of PHP's filesystem functions.

    If it's a binary file you want to offer for download, you probably also want to send the right headers so the "Save as.." dialog pops up. See the 1st answer to this question for a good example on what headers to send.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入