phazei
phazei

Reputation: 5438

How to send data in PHP without content-length?

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.

Upvotes: 4

Views: 3026

Answers (6)

Graeme Perrow
Graeme Perrow

Reputation: 57248

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.

Upvotes: 2

kender
kender

Reputation: 87171

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.

Upvotes: 10

Ilya Birman
Ilya Birman

Reputation: 10082

It’s possible that you are using gzip which waits for all content to be generated. Check your .htaccess for directives regarding this.

Upvotes: 1

Tobias
Tobias

Reputation: 7380

http content length is a SHOULD field, so you can drop it... but you have to set transfer encoding then have a look at Why "Content-Length: 0" in POST requests?

Upvotes: 0

Rich
Rich

Reputation: 36816

If you use the LiveHTTPHeaders plugin in FireFox, you can see the difference between the headers being sent by phpMyAdmin and the headers being sent by your application. I don't know specifically what you're missing, but this should give you a hint. One header that I see running a quick test is "Transfer-Encoding: chunked", and I also see that they're not sending content-length. Here's a link to the FireFox plugin if you don't already have it: LiveHTTPHeaders

Upvotes: 0

Gumbo
Gumbo

Reputation: 655319

You don’t need to set a Content-Length header field. This header field is just to tell the client amount of data it has to expected. In fact, a “wrong” value can cause that the client discards some data.

Upvotes: 0

Related Questions