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 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64