dongzhonggua4229 2010-02-04 17:13
浏览 194
已采纳

使用Content-disposition查看文件内容:使用套接字附件

I want to parse the contents of a dynamic .csv file. However, this code (obviously with duff file names, etc.):

$socket = fsockopen("www.example.com", 443);
fwrite($socket, "GET /dynamicCsv.csv?param=value HTTP/1.1
");
fwrite($socket, "Host: www.example.com
");
fwrite($socket, "Connection: close
");
fwrite($socket, "
");

while(!feof($socket)) echo fgets($socket);

...just downloads the file.

Using openssl at command line I can type the exact same request and get the following response header sent back:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-disposition: attachment; filename=dynamicCsv.csv
Pragma: public
Cache-Control: max-age=0
Set-Cookie: SESSIONID=ASDFUHN023UIN0F; Path=/; Secure
Content-Type: text/csv;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 04 Feb 2010 16:53:00 GMT

To avoid having the file download automatically I have tried to chop off the first character using substr, in the hope that maybe the browser sees the response output by fgets as a set of headers of the current document. That didn't work. Pursuing the same idea I also tried to insert a bunch of newlines before opening the socket, which didn't work either.

My two questions are:

  1. Why on earth does the browser think it should download the file?
  2. How can I stop it?

I should mention that I've only used Chrome so far, but I don't see why it would make a difference.

Many thanks, Andreas

  • 写回答

2条回答 默认 最新

  • dsb53973 2010-02-04 17:45
    关注

    Ok, I actually managed to solve it myself. For reference, instead of removing the question I add this response with the fix.

    The problem was that in

    fsockopen("www.example.com", 443);
    

    I did not specify to use the ssl protocol. So the fix is to instead type:

    fsockopen("ssl://www.example.com", 443);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决