Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send data in PHP without content-length?

Tags:

php

mysql

header

I know it's possible, but I can't seem to figure it out.

I have a mySQL query that has a couple hundred thousand results. I want to be able to send the results, but it seems the response requires content-length header to start downloading.

In phpMyAdmin, if you go to export a database, it starts the download right away, FF just says unknown file size, but it works. I looked at that code, but couldn't figure it out.

Help?

Thanks.

like image 854
phazei Avatar asked Dec 13 '22 05:12

phazei


2 Answers

this document in section 14.14 states that In HTTP, it SHOULD be sent whenever the message's length can be determined prior to being transferred, unless this is prohibited by the rules in section 4.4. This means it DOESN'T have to be sent if you can't say the size of it.

Just don't send it.

If you want to send parts of data to the browser before all data is available, do you flush your output buffer? Maybe that's what is the problem, not lack of a header?

The way you use flush is like that:

  • generate some output, which should add it to the buffer
  • flush() it, which should send current buffer to the client
  • goto 1

So, if your query returns a lot of results, you could just generate the output for, lets say, 100 or 1000 of them, then flush, and so on.

Also, to tell the client browser to attempt to save a file instead of displaying it in window, you can try using the Content-disposition: attachment header as well. See the specification here, 19.5.1 section.

like image 53
kender Avatar answered Dec 17 '22 23:12

kender


You can use chunked transfer-encoding. Basically, you send a "Transfer-encoding: chunked" header, and then the data is sent in chunked mode, meaning that you send the length of a chunk followed by the chunk. You keep repeating this until the end of data, at which point you send a zero-length chunk.

Details are in RFC 2616.

like image 39
Graeme Perrow Avatar answered Dec 17 '22 22:12

Graeme Perrow